【问题标题】:Why form isn't save values from select in asp.net mvc?为什么表单不保存从 asp.net mvc 中选择的值?
【发布时间】:2014-07-24 22:38:55
【问题描述】:

我有 asp.net mvc 4 项目,在 razor 视图中我有订单列表,每个订单都是带有值和选择输入的表单。当我尝试保存实体时,我的值输入已成功保存,但 select 未发送所选值,仅发送 null。

查看

@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var cityRepo = new Repository<City>();
    var listItemsCities = cityRepo.GetAll().Select(city => new ListItem() {
        Value = city.Id.ToString(),
        Text = city.Value
    }).ToList();
}

@foreach (var order in Model) {
                            <tr>

                                @using (Html.BeginForm("UpdateDispatcherFromForm", "Home", FormMethod.Post)) {
                                    @Html.AntiForgeryToken()
                                    @Html.ValidationSummary(true)

                                    <td>
                                        @Html.HiddenFor(x => order.Id)
                                    </td>
                                    <td>
                                        @Html.DropDownList("CityId", new SelectList(listItemsCities, "Value", "Text", order.CityId), "")
                                        @*@if (order.City != null) {
                                            @order.City.Value
                                        }*@
                                    </td>
                                    <td>
                                        @Html.EditorFor(x => order.Address)
                                        @*@order.Address*@
                                    </td>
                                    <td>
                                        <input type="submit" value="Save" />
                                    </td>
                                }
                            </tr>
                        }

控制器

[HttpPost]
        public ActionResult UpdateDispatcherFromForm(Order order) {
            _orderRepo.Update(order);
            _orderRepo.SaveChanges();

            return RedirectToAction("Dispatcher");
        }

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    改用 DropDownListFor 方法:

    http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor(v=vs.118).aspx

    根据评论中的问题进行编辑

    这里是修改后的代码:

    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var cityRepo = new Repository<City>();
    var listItemsCities = cityRepo.GetAll().Select(city => new ListItem() {
        Value = city.Id.ToString(),
        Text = city.Value
      }).ToList();
    }
    
    @foreach (var order in Model) {
                            <tr>
    
                                @using (Html.BeginForm("UpdateDispatcherFromForm", "Home", FormMethod.Post)) {
                                    @Html.AntiForgeryToken()
                                    @Html.ValidationSummary(true)
    
                                    <td>
                                        @Html.HiddenFor(x => order.Id)
                                    </td>
                                    <td>
                                        @Html.DropDownListFor(m=>m.CityId, new SelectList(listItemsCities, "Value", "Text", order.CityId), "")
                                        @*@if (order.City != null) {
                                            @order.City.Value
                                        }*@
                                    </td>
                                    <td>
                                        @Html.EditorFor(x => order.Address)
                                        @*@order.Address*@
                                    </td>
                                    <td>
                                        <input type="submit" value="Save" />
                                    </td>
                                }
                            </tr>
                        }
    

    【讨论】:

      【解决方案2】:

      您想要做的是将选择绑定到您的模型。因此,您应该改用 DropDownListFor 方法。

      要做到这一点,你应该换行

       @Html.DropDownList("CityId", new SelectList(listItemsCities, "Value", "Text", order.CityId), "")
      

      到这里

       @Html.DropDownListFor(m=>m.CityId, new SelectList(listItemsCities, "Value", "Text", order.CityId), "")
      

      看到这个MSDN articlethe answer here也是解决方案的一个很好的实现。

      【讨论】:

        猜你喜欢
        • 2012-08-31
        • 1970-01-01
        • 2016-07-07
        • 1970-01-01
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多