C# 用于复杂子对象绑定的Html.dropdownlist

C# 用于复杂子对象绑定的Html.dropdownlist,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有以下代码(ASP.NET MVC/C#)…为了简洁起见,对一些代码进行了简化 public class UserViewModel { public int UserId { get; set; } public string Name { get; set; } public AddressViewModel Address { get; set; } public IEnumerable<SelectListItem> CitySelectList

我有以下代码(ASP.NET MVC/C#)…为了简洁起见,对一些代码进行了简化

public class UserViewModel
{
   public int UserId { get; set; }
   public string Name { get; set; }
   public AddressViewModel Address { get; set; }

   public IEnumerable<SelectListItem> CitySelectList { get; set; }
}

public class AddressViewModel
{
   public int AddressId { get; set; }
   public string StreetAddress { get; set; }
   public int CityId { get; set; }
}
加载时在下拉列表中选择正确的城市(即…原始值),这样可以正常工作,但当我提交表单时,CityId属性不会用新选择的值更新…它仍然包含原始城市的id

我向UserViewModel添加了一个名为“SelectedCityId”的新属性,并将其绑定到dropdownlist,如下所示,以测试问题所在的复杂嵌套对象:

@Html.DropDownListFor(x => x.SelectedCityId, Model.CitySelectList)
当我使用此方法提交表单时,SelectedCityId确实包含正确的id,因此当对象是简单对象时,我可以看到模型绑定正常工作,但当对象是嵌套的子对象时,则不能

为了排除故障,我检查了postal code字段,因为它也绑定到Address类的子属性,但在使用类似的TextBoxFor helper时,它确实正确绑定:

@Html.TextBoxFor(x => x.Address.PostalCode)
所以,我的问题是,当表达式将控件绑定到嵌套的子属性时,为什么模型绑定似乎不适用于DropDownListFor帮助器?我确信我遗漏了一些琐碎的东西,但我没有看到它是什么,也没有在这里找到类似的答案

编辑: 以下是cshtml文件中的整个html表单

@using (Html.BeginForm("MyProfile", "Account", FormMethod.Post, new { id = "myProfileForm" }))
        {
            @Html.HiddenFor(x => x.UserId)
            @Html.HiddenFor(x => x.AddressId)
            @Html.HiddenFor(x => x.Address.CityId)

            <article class="mysettings">
                <h1>Personal details</h1>
                <table>
                    <tr>


                <th>E-mail:</th>
                    <td>@Model.Username
                        <!--edit fields-->
                        <div class="edit_field" id="field3">
                            <label for="new_email">New Email:</label>
                            @Html.TextBoxFor(x => x.Username, new { id = "new_email" })
                            <input type="submit" value="save" class="gradient-button" id="submit3"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <th>First name:</th>
                    <td>@Model.FirstName
                        <!--edit fields-->
                        <div class="edit_field" id="field1">
                            <label for="new_name">New First Name:</label>
                            @Html.TextBoxFor(x => x.FirstName, new { id = "new_name" })
                            <input type="submit" value="save" class="gradient-button" id="submit1"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field1" class="gradient-button edit">Edit</a></td>
                </tr>
                <tr>
                    <th>Last name:</th>
                    <td>@Model.LastName
                        <!--edit fields-->
                        <div class="edit_field" id="field2">
                            <label for="new_last_name">New Last Name:</label>
                            @Html.TextBoxFor(x => x.LastName, new { id = "new_last_name" })
                            <input type="submit" value="save" class="gradient-button" id="submit2"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field2" class="gradient-button edit">Edit</a></td>
                </tr>
                <tr>
                    <th>Display name:</th>
                    <td>@Model.DisplayName
                        <!--edit fields-->
                        <div class="edit_field" id="field_display">
                            <label for="new_display_name">New Display Name:</label>
                            @Html.TextBoxFor(x => x.DisplayName, new { id = "new_display_name" })
                            <input type="submit" value="save" class="gradient-button" id="submit_display"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field_display" class="gradient-button edit">Edit</a></td>
                </tr>
                <tr>
                    <th>Password:</th>
                    <td><input type="password" value="@Model.Membership.Password" disabled="disabled" style="border: none; background-color: white; padding: 0;" />
                        <!--edit fields-->
                        <div class="edit_field" id="field4">
                            <label for="new_password">New Password:</label>
                            @Html.PasswordFor(x => x.Membership.Password, new { id = "new_password" })
                            <input type="submit" value="save" class="gradient-button" id="submit4"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <th>Cell phone:</th>
                    <td>@Model.CellPhone
                        <!--edit fields-->
                        <div class="edit_field" id="field_cell">
                            <label for="new_cell">New Cell Phone:</label>
                            @Html.TextBoxFor(x => x.CellPhone, new { id = "new_cell" })
                            <input type="submit" value="save" class="gradient-button" id="submit_cell"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field_cell" class="gradient-button edit">Edit</a></td>
                </tr>
                <tr>
                    <th>Home phone:</th>
                    <td>@Model.HomePhone
                        <!--edit fields-->
                        <div class="edit_field" id="field_home">
                            <label for="new_home">New Home Phone:</label>
                            @Html.TextBoxFor(x => x.HomePhone, new { id = "new_home" })
                            <input type="submit" value="save" class="gradient-button" id="submit_home"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field_home" class="gradient-button edit">Edit</a></td>
                </tr>
                <tr>
                    <th>Street Address:</th>
                    <td>@(Model.Address != null ? Model.Address.Address1 : "No address given")
                        <!--edit fields-->
                        <div class="edit_field" id="field5">
                            <label for="new_address">New Address:</label>
                            @Html.TextBoxFor(x => x.Address.Address1, new { id = "new_address" })
                            <input type="submit" value="save" class="gradient-button" id="submit5"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field5" class="gradient-button edit">Edit</a></td>
                </tr>

                <tr>
                    <th>Town / City:</th>
                    <td>@(Model.Address != null ? Model.Address.City.Name : "No address given")
                        <!--edit fields-->
                        <div class="edit_field" id="field6">
                            <label for="new_city">New City:</label>
                            @Html.DropDownListFor(x => x.Address.CityId, Model.CitySelectList, new { @class = "f-item" })
                            <input type="submit" value="save" class="gradient-button" id="submit6"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field6" class="gradient-button edit">Edit</a></td>
                </tr>

                <tr>
                    <th>Postal Code:</th>
                    <td>@(Model.Address != null ? Model.Address.PostalCode : "No address given")
                        <!--edit fields-->
                        <div class="edit_field" id="field7">
                            <label for="new_zip">New Postal Code:</label>
                            @Html.TextBoxFor(x => x.Address.PostalCode, new { id = "new_zip" })
                            <input type="submit" value="save" class="gradient-button" id="submit7"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field7" class="gradient-button edit">Edit</a></td>
                </tr>

                <tr>
                    <th>First Admin Division (State):</th>
                    <td>@(Model.Address != null ? Model.Address.City.FirstAdminDivision.Name : "No address given")
                        <!--edit fields-->
                        <div class="edit_field" id="field8">
                            <label for="new_state">New First Admin Division (State):</label>
                            <input type="text" id="new_state"/>
                            <input type="submit" value="save" class="gradient-button" id="submit8"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field8" class="gradient-button edit">Edit</a></td>
                </tr>

                <tr>
                    <th>Country:</th>
                    <td>@(Model.Address != null ? Model.Address.City.FirstAdminDivision.Country.Name : "No address given")
                        <!--edit fields-->
                        <div class="edit_field" id="field9">
                            <label for="new_country">New Country:</label>
                            <input type="text" id="new_country"/>
                            <input type="submit" value="save" class="gradient-button" id="submit9"/>
                            <a href="#">Cancel</a>
                        </div>
                        <!--//edit fields-->
                    </td>
                    <td><a href="#field8" class="gradient-button edit">Edit</a></td>
                </tr>
            </table>

        </article>
    }
以及整个UserProfileViewModel:

public class UserProfileViewModel : BaseViewModel
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string DisplayName { get; set; }
        public string CellPhone { get; set; }
        public string HomePhone { get; set; }
        public int AddressId { get; set; }

        public AddressViewModel Address { get; set; }
        public MembershipViewModel Membership { get; set; }

        public List<TripViewModel> ConsumerTrips { get; set; }

        public List<UserReviewViewModel> UserReviews { get; set; }

        public IEnumerable<SelectListItem> CitySelectList { get; set; }
        public int SelectedCityId { get; set; }
    }
公共类UserProfileViewModel:BaseViewModel { public int UserId{get;set;} 公共字符串用户名{get;set;} 公共字符串名{get;set;} 公共字符串LastName{get;set;} 公共字符串DisplayName{get;set;} 公共字符串{get;set;} 公共字符串家庭电话{get;set;} public int AddressId{get;set;} 公共地址ViewModel地址{get;set;} 公共成员身份视图模型成员身份{get;set;} 公共列表ConsumerTrips{get;set;} 公共列表用户评论{get;set;} 公共IEnumerable CitySelectList{get;set;} public int SelectedCityId{get;set;} }
除了下拉列表之外,表单还包含属性的隐藏输入

@Html.HiddenFor(x => x.Address.CityId)
....
@Html.DropDownListFor(x => x.Address.CityId, Model.CitySelectList, ..)
实际上,您正在为同一属性传回2个值。
DefaultModelBinder
读取第一个(从原始值的隐藏输入)并设置
Address.CityId
的值。将忽略同一属性(从下拉列表中)的任何后续值


删除隐藏的输入,您的模型将与所选的值正确绑定。

您显示的效果很好,因此我怀疑您遗漏了一些重要的代码(“为了简洁而简化了一些代码”)“简洁”实际上只是删除了与地址无关的内容(减去视图本身的代码)。我现在已经添加了所有内容,如果您看到任何内容,我将非常感激。它似乎应该可以工作(我以前使用过类似的风格,没有问题)。我希望这是我犯的愚蠢错误之一,因为我离代码太近了,所以我能看到。
public class AddressViewModel
    {
        public Int32 AddressId { get; set; }
        public String Address1 { get; set; }
        public String Address2 { get; set; }
        public Int32 CityId { get; set; }
        public String PostalCode { get; set; }

        public CityViewModel City { get; set; }

    }
public class UserProfileViewModel : BaseViewModel
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string DisplayName { get; set; }
        public string CellPhone { get; set; }
        public string HomePhone { get; set; }
        public int AddressId { get; set; }

        public AddressViewModel Address { get; set; }
        public MembershipViewModel Membership { get; set; }

        public List<TripViewModel> ConsumerTrips { get; set; }

        public List<UserReviewViewModel> UserReviews { get; set; }

        public IEnumerable<SelectListItem> CitySelectList { get; set; }
        public int SelectedCityId { get; set; }
    }
@Html.HiddenFor(x => x.Address.CityId)
....
@Html.DropDownListFor(x => x.Address.CityId, Model.CitySelectList, ..)