如何在html.dropdownlist(mcv5)中绑定嵌套对象 [英] How to bind a nested object in a html.dropdownlist (mcv5)
问题描述
我有3个班级。
人,地址,国家。
人数n:1地址
地址n:1国家。
我的EF型号工作正常但是......
当我使用脚手架生成我的地址视图时,它可以工作。
和
当我使用脚手架生成我的Person并手动添加地址字段时。
下拉列表无效。
当我在debugmode中检查我的模型时,CountryId = 0.
我该怎么办?
srry如果我的englitch是可怕的坏我有点noobie
< div class = 形式的基团 >
@ Html.LabelFor(model => model.Address.IdCountry,IdCountry,htmlAttributes:new {@class =control-label col-md-2})
< div class = COL-MD-10 >
@ Html.DropDownList(IdCountry,null,htmlAttributes:new {@class =form-control})
@ Html.ValidationMessageFor(model => model.Address.IdCountry, ,new {@class =text-danger})
< / div>
< / div>
好的,CountryId是0,因为这是int值的默认值(或者它应该是IdCountry,就像你的代码中所示)。
你想要使用一个稍微不同的辅助类,Html.DropDownListFor( )。它应该如下所示:
@ Html.DropDownListFor(model = > model.IdCountry,Model.mySelectListItems, new {@class = 表单控件})
现在,您需要进行一些更改在模型方面。由于我不知道你的型号名称,我只称它为ViewModel:
public class ViewModel
{
...
public IEnumerable< selectlistitem> mySelectListItems
...
}
< / selectlistitem < span class =code-keyword>>
最后,因为我不知道你怎么样重新构建ViewModel,我们将看一下基于控制器的实现:
public ActionResult MyViewAction()
{
...
// 创建ViewModel对象后
ViewModelInstance.mySelectListItems =
MyDbContext.Countries.Select(x = >
new SelectListItem {
Value = x.Id,
Text = x.Name
})。ToArray();
...
}
有一个相当全面的演练:
使用ASP.NET MVC的DropDownListFor [ ^ ]
i have 3 classes.
Person, Address, Country.
Person n:1 Address
Address n:1 Country.
my EF model is working correctly but...
when i use scaffolding to generate my Address views it works.
and
when i use scaffolding to generate my Person and manually add the address fields.
the dropdownlist doesnt work.
when i check my model in debugmode, the CountryId is = 0.
what can i do?
srry if my englitch is terrible bad im a little noobie
<div class="form-group"> @Html.LabelFor(model => model.Address.IdCountry, "IdCountry", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("IdCountry", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Address.IdCountry, "", new { @class = "text-danger" }) </div> </div>
Okay, CountryId is coming through as 0 since that's the default value for an int value (or is it supposed to be IdCountry, as that's shown in your code).
You'll want to use a slightly different helper class, the Html.DropDownListFor(). It should look like this:
@Html.DropDownListFor(model => model.IdCountry, Model.mySelectListItems, new { @class = "form-control" })
Now, you'll need to make a couple of changes on the model side. Since I don't know your model name, I'll just call it ViewModel:
public class ViewModel { ... public IEnumerable<selectlistitem> mySelectListItems ... } </selectlistitem>
And finally, since I don't know how you're building your ViewModel, we'll look at a controller-based implementation:
public ActionResult MyViewAction() { ... // After the ViewModel object has been created ViewModelInstance.mySelectListItems = MyDbContext.Countries.Select(x => new SelectListItem { Value = x.Id, Text = x.Name }).ToArray(); ... }
There is a fairly comprehensive walkthrough at:
DropDownListFor with ASP.NET MVC[^]
这篇关于如何在html.dropdownlist(mcv5)中绑定嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!