I have a checkbox like this:
<%= Html.CheckBoxFor(x => x.IsSendingQualitySurvey) %>/>
when checking the checkbox and submitting, I get the typical 2 form values:
IsSendingQualitySurvey: true
IsSendingQualitySurvey: false
This is perfectly valid for the way the mvc modelbinder handles checkboxes.
Then in the controller action:
[HttpPost]
public ActionResult Edit(Guid id, TicketEditViewModel ticketEditViewModel)
ticketEditViewModel.IsSendingQualitySurvey
(a normal bool) is always false.
I don't have any custom model binders and it works elsewhere. Strangely enough I've had the same problem in another view quite a while ago, where I had to create the input manually, looks like this:
<input type="checkbox" value="true" name="<%=Html.NameFor(x => x.IsRequestingVisit) %>" id="<%=Html.NameFor(x => x.IsRequestingVisit) %>" />
This worked, but when I copied the exact same over to the other view, the value is still always false.
Wheres the catch?
Thanks.
EDIT Got the wrong end of the stick... sorry
Have you tried fetching the raw value out of the post data like so:
In the Controller:
[HttpPost]
public ActionResult Edit(Guid id, TicketEditViewModel ticketEditViewModel,
FormCollection fc) {
if(fc["IsSendingQualitySurvey"].Contains("true")) {
//Do something.
}
}
In the View:
<%= Html.CheckBoxFor(model => model.IsSendingQualitySurvey) %>
Hope this helps..
The model binder likely wont pickup the binding. My advice is to change your action to:
[HttpPost]
public ActionResult Edit(Guid id, TicketEditViewModel model)
The model binder wants to find properties in the Request that have a prefix that match your object name. Since there is no naming prefix on the client side, there are no properties with the prefix "ticketEditViewModel".
The alternative is to name a variable on the page:
<% var ticketEditViewModel = Model; %>
<%= Html.CheckBoxFor(model => ticketEditViewModel.IsSendingQualitySurvey) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With