与 SelectList 的模型绑定

发布于 2024-07-09 14:40:32 字数 1587 浏览 7 评论 0原文

我使用 Html.DropDownList(string NameSelectListInViewData) 方法创建一个 DropDown。 这会生成具有正确值的有效选择输入。 一切都很好。

然而,提交后,源 SelectList 中的值并未绑定。

案例: ViewData.SearchBag.FamilyCodes:

public SelectList FamilyCodes { get; set; }

生成下拉列表的 Html:

<%=Html.DropDownList("SearchBag.FamilyCodes")%>

生成的 html:

<select id="SearchBag.FamilyCodes" name="SearchBag.FamilyCodes">
    <option value=" ">Any</option>
    <option value="A">BLUE</option>
    <option value="B">BLACK</option>
    <option value="C">BEIGE</option>
    <option value="G">GREEN</option>
    <option value="O">ORANGE</option>
    <option value="P">PURPLE</option>
    <option value="R">RED</option>
    <option value="S">GRAY</option>
    <option value="U">BROWN</option>
    <option value="W">WHITE</option>
    <option value="Y">YELLOW</option>
</select>

在我的控制器中,我有一个带有参数 searchBag 的操作。

public ActionResult AdvancedSearch(SearchBag searchBag) { 
    //Do Stuff with parameters in searchBag
    return View("AdvancedSearch", searchViewData);
}

所有其他字段都可以很好地绑定,只有选择框不能。 有任何想法吗?

更新
对于未来的读者来说,阅读这篇博文可能是值得的:http://haacked.com/archive/0001/01/01/model-binding-to-a-list.aspx

I create a DropDown with the Html.DropDownList(string NameSelectListInViewData) method.
This generates a valid Select input with the correct values. And all is well.

Upon submit however, the value in the source SelectList is not bound.

Case:
ViewData.SearchBag.FamilyCodes:

public SelectList FamilyCodes { get; set; }

Html that generates the dropdown:

<%=Html.DropDownList("SearchBag.FamilyCodes")%>

Generated html:

<select id="SearchBag.FamilyCodes" name="SearchBag.FamilyCodes">
    <option value=" ">Any</option>
    <option value="A">BLUE</option>
    <option value="B">BLACK</option>
    <option value="C">BEIGE</option>
    <option value="G">GREEN</option>
    <option value="O">ORANGE</option>
    <option value="P">PURPLE</option>
    <option value="R">RED</option>
    <option value="S">GRAY</option>
    <option value="U">BROWN</option>
    <option value="W">WHITE</option>
    <option value="Y">YELLOW</option>
</select>

In my controller I have an action with a parameter searchBag.

public ActionResult AdvancedSearch(SearchBag searchBag) { 
    //Do Stuff with parameters in searchBag
    return View("AdvancedSearch", searchViewData);
}

All other fields bind just fine, only the selection boxes don't.
Any ideas?

UPDATE
For future readers it might be worth it to read this blog post: http://haacked.com/archive/0001/01/01/model-binding-to-a-list.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新一帅帅 2024-07-16 14:40:33

虽然“SearchBag.FamilyCodes”是有效的 HTML ID,但框架将尝试将每个表单项映射到同名的相应参数,并且参数名称中不能包含句点。 如果您为控件分配一个单独的 ID(Html.DropDownList 方法的不同重载),它会通过吗?

While "SearchBag.FamilyCodes" is a valid HTML id, the framework will attempt to map each form item to a corresponding parameter of the same name and you cannot have a period in a parameter name. If you assign a separate ID to the control (different overload of the Html.DropDownList method), does it come through?

陌若浮生 2024-07-16 14:40:33

SearchBag 在 html 中需要小写(searchBag 而不是 SearchBag)

SearchBag needs to be lower case in the html (searchBag rather than SearchBag)

泅渡 2024-07-16 14:40:33

我认为您需要将 FamilyCodes 的类型从 SelectList 更改为字符串。

public string FamilyCodes { get; set; }

然后提交时,FamilyCodes 的值应该是从下拉列表中选择的值。 我当前的项目中有几个这样的东西,没有任何问题。

I think you need to change the type of FamilyCodes from SelectList to string.

public string FamilyCodes { get; set; }

Then on submit the value of FamilyCodes should be the value selected from the dropdown list. I have several of these in my current project and have no problems.

风向决定发型 2024-07-16 14:40:33

昨天选择列表也有类似的问题,列表会生成正常,只是在 UpdateModel- 上会失败 =- 未绑定?

我在参数列表中找到了答案...

SelectList (
Collection - items to use in the  drop down,
ValueField - ie the primarykey as a String,
NameField  - ie the name of the thing as a String,
SelectedValue -  which is the passed in current objects FK relationship)

所以对我来说...

Country = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);

我使用 ViewModel 方法 - 并将其放在视图模型的构造函数中...

public AddressCountryViewModel(){
public SelectList Countrys {get; private set;}
public AddressCountryViewModel(Address address)
{
     Countrys = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);
}

然后我获取控制器编辑操作中的值并将其分配回正在更新的对象...

address.fkCountry = Convert.ToInt32(collection["fkCountry"]);

Had a similar issue yesterday with select lists, the list would generate fine, just that on UpdateModel- would fail =- not bound?

And i found the answer in the parameters list...

SelectList (
Collection - items to use in the  drop down,
ValueField - ie the primarykey as a String,
NameField  - ie the name of the thing as a String,
SelectedValue -  which is the passed in current objects FK relationship)

So for me ...

Country = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);

I use the ViewModel approach - and have this in the constructor of the view Model...

public AddressCountryViewModel(){
public SelectList Countrys {get; private set;}
public AddressCountryViewModel(Address address)
{
     Countrys = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);
}

I then grab the values in the controllers Edit Action and assign back to the object that is updating...

address.fkCountry = Convert.ToInt32(collection["fkCountry"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文