-1

I am developing an edit form. The form i need to create needs to be dynamic so i went down the road of using EditorForModel and have many different but similar models in behind. To customize each field, i use EditorTemplates for control like dropdowns, checkboxes and radiobuttons. The project needs to be delivered soon and i have done almost everything but stuck with few things.

Here is my Sex.cshmt as EditorTemplate

@Html.RadioButton("", "M") Male
@Html.RadioButton("", "F") Female

The Edit view doesn't have much as well.

@using (Html.BeginForm("Edit", "Editor", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)              

    @Html.HiddenFor(m => m.Id)       

    @Html.EditorForModel()    
    <input type="submit" class="btn btn-default" value="Save" />      
}

And i got a model with few properties. Very simple. Field for Sex has UIHint annotation so that it can pick up Sex editor template. And there are some Required annotation for some properties as well. These work fine.

And i got my controller action to return right type of person and return a view using a Factory. Just a standard factory with a switch statement that return right object.

public ActionResult Edit(int p)
    {
        var person= _service.personService.Get(p);            

        var canFac = new CandidateFactory(type);
        var res = canFac.CreateObject(person);

        return PartialView("_Edit", res);
    }

In the above code, res is an dynamically created object with properties. It has a property for Sex and it's a string with value of 'M' or 'F'. I know naturally radio buttons are bools but i have to map M or F to the radio buttons.

The form was generated fine. All the normal text-boxes are populated with data. But others like Radio buttons and check-boxes aren't populated. So, my question is, In my scenario, how do i pre-populate or pre-select radio-buttons with data?

Laurence
  • 7,633
  • 21
  • 78
  • 129
  • I think your answer could be here: http://stackoverflow.com/questions/18852821/how-to-bind-radio-button-with-model-data-asp-net-mvc – Oualid KTATA Aug 20 '14 at 13:06
  • @OualidKTATA thanks for your help but the answer doesn't use EditorTempates .. without using EditorTempate it works for me but i need EditorTemplate in my code. – Laurence Aug 20 '14 at 13:47

1 Answers1

3

You should make use of the html attributes:

@Html.RadioButton("M","M", Model == "M" ? new { Checked = "checked" } : null) Male
@Html.RadioButton("F","F,", Model == "F" ? new { Checked = "checked" } : null) Female
Oualid KTATA
  • 1,116
  • 10
  • 20
  • Thanks Oualid. As my RadioButtonFor is in a EditorTemplate I can't do model => model.Sex. It will give me compile time error as there is no model in the template. As Stephen suggested, i can use your code or similar one on the main view, and it works as expected but i have to make this EditorTemplate work. – Laurence Aug 20 '14 at 14:09
  • You might need to access the metadata to get the property name you annotated. Let me know if you need help on that. – Oualid KTATA Aug 20 '14 at 14:17
  • Yes .. i put the annotation on at runtime using DataAnnotationsModelMetadataProvider and overriding CreateMetaData method. Is it what you're talking about? I didn't mention that as i want to keep the question simple. But i don't think that's the problem though. I think there is something i need to put on the editor template to preselect the value. but i have no idea what i need. Thanks. – Laurence Aug 20 '14 at 14:23
  • What I mean is instead of using the Model.Sex which is not generic try to get the propertyname from ViewData.ModelMetadata.Properties collection and work with it. – Oualid KTATA Aug 20 '14 at 14:37
  • oh i see .. No .. i don't know how to get the value from ViewData.ModelMetadata thingy. Can you shed some light on? Thanks – Laurence Aug 20 '14 at 14:49
  • Hey, I updated the Answer this should work fine. I tested it in a sample project. – Oualid KTATA Aug 20 '14 at 15:32
  • yayyy ... it works now .. Thanks a lot Oualid .. (i didn't know i have access to the Model like that from editorTemplate.) – Laurence Aug 20 '14 at 15:43