Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
I want to pass notcfm value as 1 if checkbox checked other wise 0


View
HTML
@using(Ajax.BeginForm("Index", "City",
                         new
                         {
                             page = 1,

                         },
                         new AjaxOptions
                         {
                             HttpMethod = "GET",
                             UpdateTargetId = "divupdate",
                             InsertionMode = InsertionMode.Replace
                         }))
{
@Html.CheckBox("chkoptn", false, new { name = "notcfm", onChange = "$(this).closest('form').submit();" })
}

Controller

C#
public ActionResult Index(int? page, int? notcfm)
      {



}

i am getting
notcfm is null
Posted
Updated 2-Feb-14 5:12am
v2
Comments
Sampath Lokuge 2-Feb-14 7:16am    
With above code what's your problem ?
Member-515487 2-Feb-14 11:18am    
actually i am getting name as chkoptn

<input id="chkoptn" name="chkoptn" önchange="$(this).closest('form').submit();" type="checkbox" value="true">

You simply change
@Html.CheckBox("chkoptn"
to
@Html.CheckBox("notcfm ",  new { id = "chkoptn", onChange = "$(this).closest('form').submit();" })


because syntax for the helper checkbox is:-
@Html.CheckBox("Nameofthe checkbox", false, new{,,,,,,});



i hope this will works for you if not please comment.
 
Share this answer
 
When you are using a checkbox on your view in MVC then that checkbox also add a extra hidden filed for same name and when you go submit then you will get hidden filed value of check box like

HTML
<input id="notcfm" name="notcfm" onchange="$(this).closest('form').submit();" value="true" type="checkbox">
>input value="false" name="notcfm" type="hidden"></input>


When you are going to submit this form using check box then you will get two value one is by hidden field of check box and another is check box value because Check box is not updating before submission and both have different values.

Now check box and hidden filed have same name then a single request variable has both values comma separate like "true,false" but first value always for checkbox and second value for hidden filed so you can split this and get check box value.

create your View like:
HTML
<h2>Index</h2>
@using(Ajax.BeginForm("Index", "City",
                         new
                         {
                             page = 1,
 
                         },
                         new AjaxOptions
                         {
                             HttpMethod = "POST",
                             UpdateTargetId = "divupdate",
                             InsertionMode = InsertionMode.Replace
                         }))
{
@Html.CheckBox("notcfm",  new { name = "notcfm", onChange = "$(this).closest('form').submit();" })
    <div id="divupdate"></div>
}


and after that you get values of checkbox in post action method in controller:

C#
public ActionResult Index()
       {
           return View();
       }
       [HttpPost]
       public ActionResult Index(string page,FormCollection form)
       {
           bool isNotCfm = Convert.ToBoolean(form["notcfm"].Split(',')[0]);
           return View();
       }


you can know more way about how can pass values from view to controller:

Getting Data From View to Controller in MVC[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900