Controller未将Viewbag值返回到View

时间:2017-03-10 09:54:19

标签: asp.net-mvc-4

我有一个名为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>

                            }

正如您所见,Controller发送ViewBag值&#34;&#34;,它应该输入View的 else 条件。但它绕过了这两个条件。 请帮助我理解为什么控制器无法将Viewbag值发送到视图。我已经做了类似的案件,但它正在休息。 我也试过清除缓存了。 请注意,我的视图是强类型视图。

0 个答案:

没有答案