When I make checkbox checked or not on Members.aspx, the value is null in the controller(HttpPost) method.
How can i get the checkbox value in my Controller.
If I check frmValues["Is_Host.Value"] I can see "true,false"
frmValues["Is_Host"] as null
true, false means the checkbox is checke. It is the way checkboxes are handled in Asp.net Mvc. To each checkbox it is associated a hidden filed with a "false" value that has the same name. So when the checkbox is unchecked the server receive "false" from
the hidden field, while when the checkbox is cheched the servers receives both values: true from the checkbox and false from the hidden field, so you get true, false.
If one doesn't add the hidden field, the server would receive NOTHING when the checkbox is unchecked, because the Html rule is that the server receives the list of all values of all checked checkboxes having the same name.
Now if you use a Viemodel instead of reading manually the form values...the model binder do all the job of decoding the received values and you would receive a boolean in your boolean property of your ViewModel.
If write ViewModel to control these, then do my Pattern becomes MVVM?
No a ViewModel in MVVM is completely different from a view model in asp.net mvc. In asp.net mvc view models are models that are specific to the view itself. There is no binding and it is definitely not MVVM.
You already have a view model (ie a model) because you are doing this:
swapnashetti
Member
23 Points
53 Posts
Html.CheckBoxFor returning a null value from View to controller
Mar 21, 2012 06:16 AM|LINK
Hi,
I am new to MVC.
I am using
Members.aspx - View
<%:Html.CheckBoxFor(model => model.Is_Host.Value)%> in my View
Members - Controller
[HttpPost]
public ActionResult CreateMember(Member member,FormCollection frmValues)
{
member.FKRoleId = (byte)(Convert.ToInt16(frmValues["roleId"]));
membersRepository.InsertMember(member);
return View("Members", membersRepository.SPGetAllMembers());
}
When I make checkbox checked or not on Members.aspx, the value is null in the controller(HttpPost) method.
How can i get the checkbox value in my Controller.
If I check frmValues["Is_Host.Value"] I can see "true,false"
frmValues["Is_Host"] as null
Thanks.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Html.CheckBoxFor returning a null value from View to controller
Mar 21, 2012 08:36 AM|LINK
true, false means the checkbox is checke. It is the way checkboxes are handled in Asp.net Mvc. To each checkbox it is associated a hidden filed with a "false" value that has the same name. So when the checkbox is unchecked the server receive "false" from the hidden field, while when the checkbox is cheched the servers receives both values: true from the checkbox and false from the hidden field, so you get true, false.
If one doesn't add the hidden field, the server would receive NOTHING when the checkbox is unchecked, because the Html rule is that the server receives the list of all values of all checked checkboxes having the same name.
Now if you use a Viemodel instead of reading manually the form values...the model binder do all the job of decoding the received values and you would receive a boolean in your boolean property of your ViewModel.
Mvc Controls Toolkit | Data Moving Plug-in Videos
swapnashetti
Member
23 Points
53 Posts
Re: Html.CheckBoxFor returning a null value from View to controller
Mar 21, 2012 10:30 PM|LINK
Thank You SO much for your reply.
If I want to handle checkboxes with out ViewModel, Do i need to handle them as follows?
if
(frmValues["Is_Active.Value"] == "false"
)
member.Is_Active =false;
else
member.Is_Active =true;
membersRepository.InsertMember(member);
return View("Members", membersRepository.SPGetAllMembers());
And by default if want the checkbox to be checked?
<%:Html.CheckBoxFor(model => model.Is_Active.Value, new { @checked = "checked"})%> is not working?
Do I need to control this by ViewModel too?
And I have one more doubt?
If write ViewModel to control these, then do my Pattern becomes MVVM?
Please let me know, I am new to use this pattern.
Thank You.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Html.CheckBoxFor returning a null value from View to controller
Mar 21, 2012 10:58 PM|LINK
You need to
No a ViewModel in MVVM is completely different from a view model in asp.net mvc. In asp.net mvc view models are models that are specific to the view itself. There is no binding and it is definitely not MVVM.
You already have a view model (ie a model) because you are doing this:
Try changing it to this:
Blog | Twitter : @Hattan
swapnashetti
Member
23 Points
53 Posts
Re: Html.CheckBoxFor returning a null value from View to controller
Mar 21, 2012 11:15 PM|LINK
Thanks for info on MVVM.
And
<%:Html.CheckBoxFor(model => model.Is_Host.Value)%>
is just the Entity that i am adding from DAL(Entity Framework). i.e
System.Web.Mvc.ViewPage<Investorium.DAL.Member>
where "Investorium.DAL.Member" is my Entity.
I have not written any seperate ViewModel for this.
<%:Html.CheckBoxFor(model => model.Is_Host)%> I can not use this, Is_Host is a nullable field in database. Which throws me error if i use this.
i.e i am using <%:Html.CheckBoxFor(model => model.Is_Host.Value)%>
Thank You.