简体   繁体   English

Asp.net MVC2-表单后绑定

[英]Asp.net MVC2 - Form Post Binding

I can't for the life of me figure out what I'm doing wrong. 我无法终生弄清楚自己在做什么错。

I have a SearchModel class that my view inherits from 我有一个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; }
}

I have a controller called SearchController 我有一个名为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);
        }

The SearchController index page has this to render the fields 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>
<% } %>

When I modify the value of the SearchField .Value property and press the submit button, it posts to the public ActionResult Index(SearchModel searchModel) method. 当我修改SearchField .Value属性的值并按Submit按钮时,它将发布到public ActionResult Index(SearchModel searchModel)方法。

The searchModel variable contains the collection SearchFields , however only the " Value " and " FilterOperator " properties are not null. searchModel变量包含集合SearchFields ,但是只有“ Value ”和“ FilterOperator ”属性不为空。

How can I include the other properties in the post, even if I don't want to explicitly list them in the form? 即使我不想在表单中明确列出其他属性,如何在帖子中包含其他属性?

This image below shows the values being sent to the "index" display page 下图显示了发送到“索引”显示页面的值 在此处输入图片说明

This image below shows the output from POST 下图显示了POST的输出 从POST输出

If you want to have those values postbacked you need to either provide them in the view by having the fields as Hidden fields 如果要回传这些值,则需要通过将字段作为隐藏字段在视图中提供它们

Html.HiddenFor(m => m.searchFields[i].FilterOperator)

Or have some sort of factory that fills your Model with default values 或者有某种工厂可以用默认值填充模型

How can I include the other properties in the post, even if I don't want to explicitly list them in the form? 即使我不想在表单中明确列出其他属性,如何在帖子中包含其他属性?

You'll have to fetch them again or stuff them in a hiddenfield and basically let the user define your readonly data. 您将不得不再次获取它们或将它们塞入隐藏字段,并基本上让用户定义您的只读数据。

This is why I like to keep the PostModel as a property of the ViewModel. 这就是为什么我喜欢将PostModel保留为ViewModel的属性的原因。 Then my post action method only needs the PostModel parameter. 然后,我的后操作方法只需要PostModel参数。 Otherwise, like in your example, you get back this hybrid ViewModel/PostModel and you need to figure out which properties to use. 否则,就像您的示例一样,您将获得此混合ViewModel / PostModel,并且需要弄清楚要使用的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM