C# 为什么形式不是';是否在asp.net mvc中保存select中的值?

C# 为什么形式不是';是否在asp.net mvc中保存select中的值?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我有一个asp.NETMVC4项目,在razor视图中我有订单列表,每个订单都是带有值和选择输入的表单。当我尝试保存实体时,我的值输入已成功保存,但select不发送所选值,仅为null 查看 @{ ViewBag.Title = ""; Layout = "~/Views/Shared/_Layout.cshtml"; var cityRepo = new Repository<City>(); var listItemsCities = cityR

我有一个asp.NETMVC4项目,在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>
                        }

请改用DropDownListFor方法:

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

以下是为您修改的代码:

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>
                    }
ViewBag.Title=”“;
Layout=“~/Views/Shared/_Layout.cshtml”;
var cityRepo=new Repository();
var listItemsCities=cityRepo.GetAll().Select(city=>newlistItem()){
Value=city.Id.ToString(),
Text=city.Value
}).ToList();
}
@foreach(模型中的var顺序){
@使用(Html.BeginForm(“UpdateDispatcherFromForm”,“Home”,FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(x=>order.Id)
@DropDownListFor(m=>m.CityId,新的选择列表(listItemsCities,“Value”,“Text”,order.CityId),“”)
@*@如果(order.City!=null){
@秩序、城市、价值
}*@
@EditorFor(x=>order.Address)
@*@订单,地址*@
}
}

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

要做到这一点,你应该改变路线

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

 @Html.DropDownListFor(m=>m.CityId, new SelectList(listItemsCities, "Value", "Text", order.CityId), "")
看到这一点,这也是解决方案的一个很好的实现

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