Note that there are some explanatory texts on larger screens.

plurals
  1. POAbstracting the interpretation of MVC checkboxes values received by the FormsCollection object
    primarykey
    data
    text
    <p>In ASP.NET MVC a checkbox is generated by the HtmlHelper code here:</p> <pre><code>&lt;%= Html.CheckBox("List_" + mailingList.Key, true) %&gt; </code></pre> <p>as this HTML:</p> <pre><code>&lt;input id="List_NEW_PRODUCTS" name="List_NEW_PRODUCTS" type="checkbox" value="true" /&gt; &lt;input name="List_NEW_PRODUCTS" type="hidden" value="false" /&gt; </code></pre> <p>In case you're wondering why is there an extra hidden field? - then <a href="https://stackoverflow.com/questions/220020#479220">read this</a>. Its definitely a solution that makes you first think 'hmmmmm' but then you realize its a pretty elegant one.</p> <p>The problem I have is when I'm trying to parse the data on the backend. Well its not so much of a problem as a concern if anything in future were to change in the framework.</p> <p>If I'm using the built in binding everything is great - its all done for me. But in my case I'm dynamically generating checkboxes with unknown names and no corresponding properties in my model.</p> <p>So i end up having to write code like this :</p> <pre><code>if (forms["List_RETAIL_NOTIFICATION"] == "true,false") { } </code></pre> <p>or this: </p> <pre><code>if (forms.GetValues("List_RETAIL_NOTIFICATION")[0] == "true") { } </code></pre> <p>Both of which i still look at and cringe - especially since theres no guarantee this will always be the return value. I'm wondering if theres a way to access the layer of abstraction used by the model binders - or if I'm stuck with my controller 'knowing' this much about HTTP POST hacks. </p> <p>Yes I'm maybe being a little picky - but perhaps theres a better clever way using the model binders that I can employ to read dynamically created checkbox parameters. </p> <p>In addition i was hoping this this post might help others searcheing for : "true,false". Even though I knew why it does this I just forgot and it took me a little while to realize 'duh'.</p> <p><hr> FYI: I tried another few things, and this is what I found :</p> <p><code>forms["List_RETAIL_NOTIFICATION"]</code> evaluates to "true,false" <code>forms.GetValues("List_RETAIL_NOTIFICATION")[0]</code> evaluates to "true" <code>(forms.GetValue("List_RETAIL_NOTIFICATION").RawValue as string[])[0]</code> evaluates to "true" <code>forms.GetValues("List_RETAIL_NOTIFICATION").FirstOrDefault()</code> evaluates to "true"</p>
    singulars
    1. This table or related slice is empty.
    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.
    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