gpt4 book ai didi

asp.net-mvc - MVC2 模型绑定(bind)枚举?

转载 作者:行者123 更新时间:2023-12-02 18:03:59 24 4
gpt4 key购买 nike

好吧,我真的对 MVC 中的模型绑定(bind)相当陌生,我的问题是:

如果我有一个带有 IEnumerable 属性的模型,我如何使用 HtmlHelper 来使用它,以便我可以提交到采用该模型类型的操作。

模型示例:

public class FooModel {
public IEnumerable<SubFoo> SubFoos { get; set; }
}
public class SubFoo {
public string Omg { get; set; }
public string Wee { get; set; }
}

查看片段:

<%foreach(var subFoo in Model.SubFoo) { %>
<label><%:subfoo.Omg %></label>
<%=Html.TextBox("OH_NO_I'M_LOST") %>
<%} %>

最佳答案

而不是 IEnumerable<SubFoo>你可以使用数组:

public class FooModel {
public SubFoo[] SubFoos { get; set; }
}

然后在你看来:

<% for (var i = 0; i < Model.SubFoo.Length; i++) { %>
<label><%:subfoo.Omg %></label>
<%=Html.TextBoxFor(x => x.SubFoo[i].Omg) %>
<%} %>

另一种可能性是保留 IEnumerable<SubFoo>但在这种情况下,您不能使用强类型帮助器:

<% for (var i = 0; i < Model.SubFoo.Count(); i++) { %>
<label><%:subfoo.Omg %></label>
<%=Html.TextBox("SubFoo[" + i + "].Omg") %>
<%} %>

关于asp.net-mvc - MVC2 模型绑定(bind)枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com