Asp.net mvc 4 控制器未将Viewbag值返回到视图

Asp.net mvc 4 控制器未将Viewbag值返回到视图,asp.net-mvc-4,Asp.net Mvc 4,我有一个名为CallAllocation的控制器和一个View CallAllocation。 首先,当页面加载时,仅会出现两个下拉列表,其中包含一个提交按钮。提交时,页面将根据两个下拉列表中的选择填充其余的详细信息。为了实现这一点,我编写了以下代码: 请关注ViewBag.IsValid条件 我的控制器就像 [HttpGet] public ActionResult CallAllocation() { ViewBag.UserName = User.I

我有一个名为CallAllocation的控制器和一个View CallAllocation。 首先,当页面加载时,仅会出现两个下拉列表,其中包含一个提交按钮。提交时,页面将根据两个下拉列表中的选择填充其余的详细信息。为了实现这一点,我编写了以下代码:

请关注ViewBag.IsValid条件

我的控制器就像

    [HttpGet]
    public ActionResult CallAllocation()
    {
        ViewBag.UserName = User.Identity.Name.ToString();
        try
        {
            var NatureList = (from a in dataContext.CallNatures
                              select a);

            Allocation Allocate = new Allocation();

            Allocate.CallNaturelist = NatureList.ToList();

            ViewData["SelectedTicket"] = 0;

            Allocate.CallTicketList = null;

            ViewBag.IsValid = "";

            return View(Allocate);
        }
        catch (Exception ex)
        {
            TempData["ErrMsg"] = ex.Message;
            return RedirectToAction("ShowError");
        }
    }
   @using (Html.BeginForm("CallAllocation", "Home", FormMethod.Post, new { id = "FrmCallAllocate"}))
                            {    
                                 @Html.AntiForgeryToken()
                                 @Html.ValidationSummary()
                                 @Html.Raw(ViewBag.ErrMsg)

                                <div class="row ">
                                    <div class="col-sm-12">
                                        @if (ViewBag.IsVaild == "True")
                                        {
                                            @Html.DropDownListFor(m => m.SelectedCallNature, new SelectList(Model.CallNaturelist, "CallNatureID", "Description"),
                                                            "Select Nature", new { style = "width:250px", @class = "dropdown1", @readonly = "readonly;" })   

                                            @Html.DropDownList("CallTicket", new SelectList(string.Empty, "CallTicketNumber", "CallTicketNumber"), "Select Call Ticket",
                                                        new { style = "width:250px", @class = "dropdown1" , @readonly = "readonly;" })   


                                                    <input type="submit" value="Get Ticket" style="background-color:#C5C5C5"/> 

                                        }
                                        else 
                                        {

                                         @Html.DropDownListFor(m => m.SelectedCallNature, new SelectList(Model.CallNaturelist, "CallNatureID", "Description"),
                                                            "Select Nature", new { style = "width:250px", @class = "dropdown1" })   

                                         @Html.DropDownList("CallTicket", new SelectList(string.Empty, "CallTicketNumber", "CallTicketNumber"), "Select Call Ticket",
                                                        new { style = "width:250px", @class = "dropdown1"})   


                                                    <input type="submit" value="Get Ticket" style="background-color:#C5C5C5"/> 
                                        }
                                    </div>
                                </div>

                            }
我的观点是

    [HttpGet]
    public ActionResult CallAllocation()
    {
        ViewBag.UserName = User.Identity.Name.ToString();
        try
        {
            var NatureList = (from a in dataContext.CallNatures
                              select a);

            Allocation Allocate = new Allocation();

            Allocate.CallNaturelist = NatureList.ToList();

            ViewData["SelectedTicket"] = 0;

            Allocate.CallTicketList = null;

            ViewBag.IsValid = "";

            return View(Allocate);
        }
        catch (Exception ex)
        {
            TempData["ErrMsg"] = ex.Message;
            return RedirectToAction("ShowError");
        }
    }
   @using (Html.BeginForm("CallAllocation", "Home", FormMethod.Post, new { id = "FrmCallAllocate"}))
                            {    
                                 @Html.AntiForgeryToken()
                                 @Html.ValidationSummary()
                                 @Html.Raw(ViewBag.ErrMsg)

                                <div class="row ">
                                    <div class="col-sm-12">
                                        @if (ViewBag.IsVaild == "True")
                                        {
                                            @Html.DropDownListFor(m => m.SelectedCallNature, new SelectList(Model.CallNaturelist, "CallNatureID", "Description"),
                                                            "Select Nature", new { style = "width:250px", @class = "dropdown1", @readonly = "readonly;" })   

                                            @Html.DropDownList("CallTicket", new SelectList(string.Empty, "CallTicketNumber", "CallTicketNumber"), "Select Call Ticket",
                                                        new { style = "width:250px", @class = "dropdown1" , @readonly = "readonly;" })   


                                                    <input type="submit" value="Get Ticket" style="background-color:#C5C5C5"/> 

                                        }
                                        else 
                                        {

                                         @Html.DropDownListFor(m => m.SelectedCallNature, new SelectList(Model.CallNaturelist, "CallNatureID", "Description"),
                                                            "Select Nature", new { style = "width:250px", @class = "dropdown1" })   

                                         @Html.DropDownList("CallTicket", new SelectList(string.Empty, "CallTicketNumber", "CallTicketNumber"), "Select Call Ticket",
                                                        new { style = "width:250px", @class = "dropdown1"})   


                                                    <input type="submit" value="Get Ticket" style="background-color:#C5C5C5"/> 
                                        }
                                    </div>
                                </div>

                            }
@使用(Html.BeginForm(“CallAllocation”,“Home”,FormMethod.Post,new{id=“frmcalallocate”}))
{    
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.Raw(ViewBag.ErrMsg)
@如果(ViewBag.IsVaild==“True”)
{
@Html.DropDownListFor(m=>m.SelectedCallNature,新建SelectList(Model.CallNaturelist,“CallNatureID”,“Description”),
“选择自然”,新建{style=“width:250px”、@class=“dropdown1”、@readonly=“readonly;”
@Html.DropDownList(“CallTicket”、新选择列表(string.Empty、“CallTicketNumber”、“CallTicketNumber”)、“Select Call Ticket”,
新的{style=“width:250px”、@class=“dropdown1”、@readonly=“readonly;”})
}
其他的
{
@Html.DropDownListFor(m=>m.SelectedCallNature,新建SelectList(Model.CallNaturelist,“CallNatureID”,“Description”),
“选择自然”,新的{style=“width:250px”,@class=“dropdown1”})
@Html.DropDownList(“CallTicket”、新选择列表(string.Empty、“CallTicketNumber”、“CallTicketNumber”)、“Select Call Ticket”,
新的{style=“width:250px”,@class=“dropdown1”})
}
}
正如您所看到的,控制器发送ViewBag值“”,并且它应该进入视图的else条件。但它绕过了这两个条件。 请帮助我了解控制器无法将Viewbag值发送到视图的原因。我也做过类似的案例,但在其他情况下仍然有效。 我也尝试过清除缓存。
请注意,我的视图是强类型视图。

您的意思是它绕过了这两个条件?而
IsValid
(控制器)与
isavaild
(视图)不同,旁路意味着它没有进入else条件。你所说的IsValid(控制器)和IsAvaild(视图)是什么意思?仔细看看!(不同的拼写)是什么让您认为它没有进入
else
块代码是相同的(除了一个无意义且无效的
readonly
属性-没有只读
)那么为什么你甚至有一个
if/else
块呢?那是一个愚蠢的键入错误。