2

I have a form in my asp.net mvc view as follow:

 <%using (Html.BeginForm("SearchBorrowed", "Admin", FormMethod.Get))
          { %>
        <%: Html.TextBox("searchTerm", Request.QueryString["searchterm"])%>
        <input type="submit" value="Search" />
        <br />
        Is Returned :
        <%:Html.CheckBox("IsReturned")%>
        <%} %>

and here is the 'SearchBorrowed' action:

public ActionResult SearchBorrowed(bool IsReturned=false, string searchTerm = null)
        {
            IEnumerable<BorrwoinfInfo> bs;

            //...Get from repository

            return View(bs.ToList());

        }

and finally routing settings :

  routes.MapRoute(
            "SearchBorrowed", // Route name
            "{controller}/{action}/{*searchTerm}", // URL with parameters
            new
            {
                controller = "Admin",
                action = "SearchBorrowed",

                searchTerm = UrlParameter.Optional
            } // Parameter defaults

when I submit the form without checking 'IsReturned' Checkbox, it returns result and the url gets as follow :

.../SearchBorrowed?searchterm=&IsReturned=false

But when I check IsReturned' Checkbox, the urls gets like this:

.../SearchBorrowed?searchterm=s&IsReturned=true&IsReturned=false

Why there is two IsReturned in above url ?!

How Could I fix this ?

2
  • I would recommend using a strongly typed model. Commented Jan 3, 2012 at 16:16
  • This is a search form inside a view which gets it model as 'IEnumerable<BorrwoinfInfo>' so I cant bind checkbox to model. If I want to do that, I have to use ViewModel, Am I Right? Commented Jan 3, 2012 at 16:35

1 Answer 1

2

Why there is two IsReturned in above url ?!

Because the Html.CheckBox helper generates an additional hidden input field with the same name as the checkbox. If you look at the generated HTML you will see that the helper generated the following 2 input fields:

<input type="checkbox" name="IsReturned" id="IsReturned" value="true" checked="checked" />
<input type="hidden" name="IsReturned" id="IsReturned" value="false" />

This is by design. This helper is intended to be bound to a boolean property on your view model. When a checkbox field is not checked no value is sent to the server, so if there was not no hidden field you wouldn't be able to bind it to a boolean field.

If you don't want this hidden field you could either write a custom helper or generate the checkbox field manually.

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

2 Comments

I replaced that with '<input type="checkbox" name="IsReturned"/>' but during debugging, I check 'IsReturned'paramater in 'SearchBorrowed' Action, and I see the value has not been affected by checkbox value. Wy ?
@persianDev, that's because you didn't specify a value for your checkbox: <input type="checkbox" name="IsReturned" value="true" />. Notice the value="true" attribute.

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.