views:

106

answers:

1

Hi,

I'm new to MVC. I've got this all working nicely except when the page loads the checkbox is not set to the value of the object from the model. I am certainly missing something simple - hope you can point it out to me.

I'm using a ViewModel class for the view, so my view page has this code:

<%: Html.CheckBox("IsX", Model.Contact.IsX) %>

I've been looking through the available overloads but just can't figure out what I need to put in there...

Any help greatly appreciated.

Tim.

A: 

Silly question, are you returning the viewmodel from the action?

eg

Function SomeAction(Moel as ViewModelType) As ActionResult
    If Model.IsValid()
        '' Do Stuff
    Else
        Return view(Model)
    End If
End Function

Also, in my code, I use the following syntax:

<%: Html.CheckBoxFor((x) => x.CheckboxPropertyOnModelName) %>
  1. It's strongly typed an handles the naming/etc. itself.
  2. Using the <%: syntax automatically encodes html characters in your own strings but doesn't affect HTMLStrings returned from various .Net functions (like CheckboxFor()).
Basiclife
Good question - and yes: public ActionResult Edit(int id) { Contact contact = repo.GetContact(id); return View(new ContactFormViewModel(contact)); }My other Html controls do their job - e.g. my SelectList's render with the correct value selected...
nulliusinverba
Aded alternate syntax - give that a whirl (I'm more familiar with Lambdas in VB but think that should be correct in C#)
Basiclife
Beautiful - works a treat. Thanks, mate!
nulliusinverba
Further testing has indicated that this new way of doing it isn't binding properly (won't update on post). This may very well be due to the fact that the property to bind to isn't in my LINQ class, but is a composite property defined in relation to other properties. Do you therefore have any further ideas?
nulliusinverba
Can you give details of the field on your model? The way I understand it, MVC creates a new instance of your model and them maps the posted data to the properties with the same name on your model - So if your model has a getter but not a setter, it won't map properly.To test if this is a result of my syntax or if it's a problem with the model, try grabbing the HTML for 2 fields (one which works now, one which doesn't) and hard-code them in HTML. See what happens when you post the form back - I'm guessing you'll ge tthe same result as now indicating a model issue.
Basiclife
Thanks for getting back to me. I do have getter and setter, and I can get and set through my syntax above (is just it won't render the value on page load - hence why I was looking to overloads on the checkbox method). The get method queries the database for the existence of a specific record based on the id of the Contact object and returns true or false. The set either creates or destroys such record, depending on the value. Cheers.
nulliusinverba
Hmmm okay - interesting. Can you try something: Assuming your peropty is called "DBCheck" - Can you put a breakpoint on your HTTPPost action and tell me what's returned if you query Request("DBCheck") in the watch window? This should give us the raw POST data for that field.Next, can you manually put a checkbox on the view with a name of DBCheck (and remove the CheckboxFor() line) - then submit your form again back and see if the model is updated? If one works but not the other, we can compare the difference in POST data and go from there...
Basiclife
I can report the following. I have a record set to FALSE. In the edit view I set checkbox to true and post with breakpoint early in POST action. Trying the lamba syntax (<%= Html.CheckBoxFor((x) => x.Contact.IsX.Value)), stepping all the way through the POST action the value always remains false. Stepping through the original syntax (<%: Html.CheckBox("IsX", Model.Contact.IsX) %>), the value becomes TRUE in the POST action, and the model is updated accordingly. Thanks again for your help.
nulliusinverba
Ok, so to confirm - my method shows correctly on the page, yours doesn't but yours works on post-back an mine doesn't? It must be generating different HTML with your method and mine - can you compare the 2 and identify the differences? We should be able to customise the html generated by mine to match yours but I'm surprised we're having to o all this - it feels like we're missing something obvious. For the sake of argument, can you try putting a checkbox on the model itself rather than nested in a Contact? how does that behave?
Basiclife
I can confirm your description of the problem. The HTML is different: (1) there is extra property, 'checked="checked"' on the lambda-generated html; (2) 'id', 'name' properties differ in that lambda-generated is "Contact_IsX_Value" where non-lambda is "IsX". I will have to ask what you mean by "not nested in a Contact." I've tested it outside the "using (Html.BeginForm())" tags for the same result. Cheers.
nulliusinverba