Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.net MVC2 - Form Post Binding
    primarykey
    data
    text
    <p>I can't for the life of me figure out what I'm doing wrong.</p> <p>I have a <code>SearchModel</code> class that my view inherits from</p> <pre><code> public class SearchModel { public String Something { get; set; } public List&lt;SearchField&gt; 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; } } </code></pre> <p>I have a controller called <code>SearchController</code></p> <pre><code> 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); } </code></pre> <p>The SearchController index page has this to render the fields</p> <pre><code>&lt;% using (Html.BeginForm()) {%&gt; &lt;%= Html.TextBox("Something", Model.Something) %&gt; &lt;% for (int i = 0; i &lt; Model.SearchFields.Count; i++) { %&gt; &lt;%= Html.Label(Model.SearchFields[i].DisplayName) %&gt; &lt;%= Html.DropDownListFor(x =&gt; x.SearchFields[i].FilterOperator, Model.SearchFields[i].FilterOperator.ToSelectList(), new { @class = "textField" })%&gt; &lt;%= Html.TextBoxFor(x =&gt; x.SearchFields[i].Value) %&gt; &lt;%= Html.ValidationMessageFor(x =&gt; x.SearchFields[i].Value) %&gt; &lt;% } %&gt; &lt;button type="submit" value="Search" class="SearchBtn"&gt; Search&lt;/button&gt; &lt;% } %&gt; </code></pre> <p>When I modify the value of the SearchField .Value property and press the submit button, it posts to the <code>public ActionResult Index(SearchModel searchModel)</code> method.</p> <p>The searchModel variable contains the collection <code>SearchFields</code>, however only the "<code>Value</code>" and "<code>FilterOperator</code>" properties are not null.</p> <p>How can I include the other properties in the post, even if I don't want to explicitly list them in the form?</p> <p>This image below shows the values being sent to the "index" display page <img src="https://i.stack.imgur.com/VtjFE.png" alt="enter image description here"></p> <p>This image below shows the output from POST <img src="https://i.stack.imgur.com/XmSz6.png" alt="Output From POST"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload