Dropdownlistfor 不会显示正确的选择 [英] Dropdownlistfor will not show the correct selection

查看:21
本文介绍了Dropdownlistfor 不会显示正确的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在循环中有三个 dropdownlistfor,它们没有显示来自数据库的正确值.它们始终默认为第一个条目.我已经检查并仔细检查了数据库,并确认它应该是列表中的第二个.该列表也已正确创建.我错过了什么?

 @foreach(Model.Customer.CustomerMeasurementProfiles.Where(m => m.DeletedDate == null) 中的CustomerMeasurementProfile oProfile){<div class="valuesforoneprofile form-group form-group-tight col-md-2"><div class="col-md-11" >@Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].Id", i), oProfile.Id)@Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].CustomerId", i), oProfile.CustomerId)@Html.TextBox(string.Format("Customer.CustomerMeasurementProfiles[{0}].Name", i), oProfile.Name, new { @class = "form-control input-sm" })

<div class="col-md-11" style="text-align:center">@Html.CheckBox(string.Format("DeleteProfiles[{0}]", i), Model.DeleteProfiles[i])

<div class="col-md-11" style="padding-top:4px;">@Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"), new { @class = "form-control input-sm-select" })

<div class="col-md-11" style="padding-top:4px;">@Html.DropDownListFor(m => oProfile.BodyTypePostureId, new SelectList(Model.BodyTypePosture, "Id", "Name"), new { @class = "form-control input-sm-select" })

<div class="col-md-11" style="padding-top:4px;">@Html.DropDownListFor(m => oProfile.BodyTypeChestId, new SelectList(Model.BodyTypeChest, "Id", "Name"), new { @class = "form-control input-sm-select" })

解决方案

如果要设置 Model 中的选定值.你需要这样做:

@Html.DropDownListFor(m => oProfile.BodyTypeShoulderId,新的 SelectList(Model.BodyTypeShoulders,ID",名称",oProfile.BodyTypeShoulderId),new { @class = "form-control input-sm-select" })

上面的代码将下拉选择的值设置为当前Model对象BodyTypeShoulderId

DropDownListFor 的第一个参数告诉在表单后下拉选择的值将与在那里设置的模型属性映射(我们传递 m => oProfile.BodyTypeShoulderId) 但这不会设置选定的值.

要设置选定的值,您必须使用 SelectList 类的重载,即object selectedValue

I have three dropdownlistfor in a loop that do not show the correct value from the DB. They always default to the first entry. I have checked and double checked the DB and verified that it should be the second one in the list. The list is also created correctly. What am I missing?

        @foreach (CustomerMeasurementProfile oProfile in Model.Customer.CustomerMeasurementProfiles.Where(m => m.DeletedDate == null))
        {

            <div class="valuesforoneprofile form-group form-group-tight col-md-2">
                <div class="col-md-11" >
                    @Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].Id", i), oProfile.Id)
                    @Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].CustomerId", i), oProfile.CustomerId)
                    @Html.TextBox(string.Format("Customer.CustomerMeasurementProfiles[{0}].Name", i), oProfile.Name, new { @class = "form-control input-sm" })
                </div>
                <div class="col-md-11" style="text-align:center">
                    @Html.CheckBox(string.Format("DeleteProfiles[{0}]", i), Model.DeleteProfiles[i])
                </div>
                <div class="col-md-11" style="padding-top:4px;">
                    @Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"), new { @class = "form-control input-sm-select" }) 
                </div>
                <div class="col-md-11" style="padding-top:4px;">
                    @Html.DropDownListFor(m => oProfile.BodyTypePostureId, new SelectList(Model.BodyTypePosture, "Id", "Name"), new { @class = "form-control input-sm-select" })
                </div>
                <div class="col-md-11" style="padding-top:4px;">
                    @Html.DropDownListFor(m => oProfile.BodyTypeChestId, new SelectList(Model.BodyTypeChest, "Id", "Name"), new { @class = "form-control input-sm-select" }) 
                </div>

解决方案

If you want to set the selected value that is coming in Model. You need to do it like this:

@Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, 
                           new SelectList(Model.BodyTypeShoulders, 
                                          "Id", 
                                          "Name",
                                          oProfile.BodyTypeShoulderId), 
                           new { @class = "form-control input-sm-select" })

The above code will set the dropdown selected value to whatever is in the current Model object BodyTypeShoulderId

The first argument of DropDownListFor tells that on form post drop down selected value will be mapped with the Model property which is set there (we are passing m => oProfile.BodyTypeShoulderId) but this not sets selected Value.

For setting selected value you have to pass SelectList fourth parameter using this overload of SelectList class which is object selectedValue

这篇关于Dropdownlistfor 不会显示正确的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆