0

enter image description here I am stuck up in a pretty basic page while reading form collection on post. When I check the IsChecked checkbox, in the post action. I am getting "true, false" in the FormCollection. My goal is to obtain the string in below code and then parse it to boolean.

I have not idea where is the bug, can you please help?

enter image description here

Post Action:

[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        var checkedd = collection["IsChecked"].ToString();
        var name = collection["Name"].ToString();

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Model:

public class Product
{
    public bool IsChecked { get; set; }
    [Required]
    public string Name { get; set; }
}

View:

<% using (Html.BeginForm()) { %>
    <%: Html.AntiForgeryToken() %>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Product</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.IsChecked) %>
        </div>
        <div class="editor-field">
            <%: Html.CheckBoxFor(model => model.IsChecked) %>
            <%: Html.ValidationMessageFor(model => model.IsChecked) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Name) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.Name) %>
            <%: Html.ValidationMessageFor(model => model.Name) %>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>
5
  • How about having that method take Product as parameter instead of FormCollection and have your page bound to the model? Commented Sep 14, 2014 at 19:17
  • @shahkalpesh, Model binding is what I use in recent applications, I had been working on old code, where I faced this issue. This sample application was just to learn how FormCollection works :-) Commented Sep 14, 2014 at 19:20
  • Going to old way of doing things, does Request["IsChecked"] help? I don't know the exact syntax. This is the classic asp way to extract form post values. Commented Sep 14, 2014 at 19:23
  • @shahkalpesh Request["IsChecked"] also returns "true,false" Commented Sep 14, 2014 at 19:47
  • In that case, something is wrong with your form. Do a view source on it to see if there is more than 1 element with name IsChecked? I suppose that could be the reason. Check the content being posted using IE dev toolbar, network tab, start capturing (before you click Submit). Commented Sep 14, 2014 at 19:49

1 Answer 1

1

Since you passing a model to you view, why are you using FormCollection? if you change you POST method to

[HttpPost]
public ActionResult Create(Product model)
{

you model will be correctly bound.

The reason you are getting this value for the IsChecked is that the CheckBoxFor helper renders 2 controls - <input type="checkbox" ..> and <input type="hidden" ...>.

Because unchecked checkboxes do not post back, the 2nd hidden input ensures a value of false is posted back when its unchecked. The default model binder reads the first value matching the property name and (ignores the second if it exists).

If you really want to use FormCollection, then don't use CheckBoxFor - just manually include the html for a checkbox. Then if the value exists in FormCollection, it must be true, otherwise it must be false

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, two input elements: Two input elements: jQuery un-obtrusive validation has been stopping me to submit inline-edit to a grid, if unless I check all check-boxes on the grid.
Stephen Could you help me on this question please: stackoverflow.com/questions/25831229/…
Sure, but give me half an hour - can you post the html in that question as per comments
@Abhijeet, Note that one of the elements is a checkbox but the other is a hidden input (not a checkbox) - its just there to ensure a value is posted back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.