我不能为我的生活弄清楚我做错了什么。Asp.net MVC2 - Form Post Binding
我有一个SearchModel
类,我认为从
public class SearchModel
{
public String Something { get; set; }
public List<SearchField> SearchFields { get; set; }
}
public class SearchField
{
[XmlIgnore]
public Boolean Include { get; set; }
[XmlAttribute("Required")]
public Boolean Required { get; set; }
[XmlAttribute("Field")]
public String FieldName { get; set; }
[XmlText]
public String DisplayName { get; set; }
[XmlIgnore]
public FilterMethod FilterOperator { get; set; }
[XmlIgnore]
public String Value { get; set; }
}
继承了我所说的控制器SearchController
public ActionResult Index()
{
SearchModel model = new SearchModel
{
Something = "Hello",
SearchFields = customer.Config.Fields
};
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(SearchModel searchModel)
{
return View("Index", searchModel);
}
的SearchController索引页有这样一段呈现的字段
<% using (Html.BeginForm())
{%>
<%= Html.TextBox("Something", Model.Something) %>
<% for (int i = 0; i < Model.SearchFields.Count; i++)
{
%>
<%= Html.Label(Model.SearchFields[i].DisplayName) %>
<%= Html.DropDownListFor(x => x.SearchFields[i].FilterOperator, Model.SearchFields[i].FilterOperator.ToSelectList(), new { @class = "textField" })%>
<%= Html.TextBoxFor(x => x.SearchFields[i].Value) %>
<%= Html.ValidationMessageFor(x => x.SearchFields[i].Value) %>
<% } %>
<button type="submit" value="Search" class="SearchBtn">
Search</button>
<% } %>
当我修改SearchF的值时输入.Value属性并按下提交按钮,它发布到public ActionResult Index(SearchModel searchModel)
方法。
searchModel变量包含集合SearchFields
,但只有“Value
”和“FilterOperator
”属性不为空。
即使我不想在表单中明确列出它们,我如何在帖子中包含其他属性?
下面这张图片显示正在发送到“指数”显示页面
值低于此图片显示了POST
当您使用该模板时,HTML输出看起来如何?你有索引输入字段吗?即类似 CodingInsomnia 2011-02-17 15:45:48
如果您不想在表单中列出它们,我们可以假设您不希望用户更改它们。如果是这种情况,您可以根据您需要的任何选择标准在返回时将这些值填充到控制器中。或者像Rene展示的那样。 – Webjedi 2011-02-17 15:54:36
@CodingInsomnia:是的,他们是索引输入字段:name =“SearchFields [0]。价值“ – 2011-02-17 15:56:24