Dropdownlistfor不会显示正确的选择 [英] Dropdownlistfor will not show the correct selection
问题描述
我有一个三个筋斗dropdownlistfor不显示从数据库中正确的值。他们总是默认为第一个条目。我已经检查和双重检查数据库,并证实它应该是第二个在列表中。也是正确创建列表。我缺少什么?
@foreach(CustomerMeasurementProfile OProfile的在Model.Customer.CustomerMeasurementProfiles.Where(M = GT; m.DeletedDate == NULL))
{ < DIV CLASS =valuesforoneprofile形式组的形式组紧COL-MD-2>
< DIV CLASS =COL-MD-11>
@ Html.Hidden(的String.Format(Customer.CustomerMeasurementProfiles [{0}]标识,I),oProfile.Id)
@ Html.Hidden(的String.Format(Customer.CustomerMeasurementProfiles [{0}]客户编号I),oProfile.CustomerId)
@ Html.TextBox(的String.Format(Customer.CustomerMeasurementProfiles [{0}]。名称,I),oProfile.Name,新{@class =的形式控制输入-SM})
< / DIV>
< DIV CLASS =COL-MD-11的风格=文本对齐:中心>
@ Html.CheckBox(的String.Format(DeleteProfiles [{0}]我),Model.DeleteProfiles [I])
< / DIV>
< DIV CLASS =COL-MD-11的风格=填充顶:4PX;>
@ Html.DropDownListFor(M = GT; oProfile.BodyTypeShoulderId,新的SelectList(Model.BodyTypeShoulders,ID,姓名),新{@class =的形式控制输入-SM-选择})
< / DIV>
< DIV CLASS =COL-MD-11的风格=填充顶:4PX;>
@ Html.DropDownListFor(M = GT; oProfile.BodyTypePostureId,新的SelectList(Model.BodyTypePosture,ID,姓名),新{@class =的形式控制输入-SM-选择})
< / DIV>
< DIV CLASS =COL-MD-11的风格=填充顶:4PX;>
@ Html.DropDownListFor(M = GT; oProfile.BodyTypeChestId,新的SelectList(Model.BodyTypeChest,ID,姓名),新{@class =的形式控制输入-SM-选择})
< / DIV>
如果您想设置所选的值,在型号来。你需要做的是这样的:
@ Html.DropDownListFor(M = GT; oProfile.BodyTypeShoulderId,
新的SelectList(Model.BodyTypeShoulders,
ID,
名称,
oProfile.BodyTypeShoulderId)
新{@class =的形式控制输入-SM-选择})
以上code将设置下拉列表中选择值无论是在当前的型号对象 BodyTypeShoulderId
DropDownListFor
的第一个参数告诉表格后下拉选择的价值将被设置在那里的Model属性映射(我们传递 M => oProfile.BodyTypeShoulderId
),但这不是集中选择值
有关设置选定的值,你必须通过的SelectList
使用的这个超负荷的SelectList类的是对象了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屋!