0

I have an enum in model. I tried to created radio buttons for this enum in my view. That works fine and one checkbox is checked according to model value (like x o o).

@Html.RadioButtonFor(model => model.Data.CementType, ReMod.CementType.R, new { @id = "CementTypeR" })
<label for="CementTypeR">R</label>
@Html.RadioButtonFor(model => model.Data.CementType, ReMod.CementType.N, new { @id = "CementTypeN" })
<label for="CementTypeN">N</label>
@Html.RadioButtonFor(model => model.Data.CementType, ReMod.CementType.S, new { @id = "CementTypeS" })
<label for="CementTypeS">S</label>

However after I created EditTemplate for enum

@model ReMod.CementType

@Html.RadioButtonFor(model => model, ReMod.CementType.R, new { @id = "CementTypeR" })
<label for="CementTypeR">R</label>
@Html.RadioButtonFor(model => model, ReMod.CementType.N, new { @id = "CementTypeN" })
<label for="CementTypeN">N</label>
@Html.RadioButtonFor(model => model, ReMod.CementType.S, new { @id = "CementTypeS" })
<label for="CementTypeS">S</label>

and called this template from view

@Html.EditorFor(model => model.Data.CementType)

, than is not any checkbox checked (like o o o).

Thank you for any advice ...

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

0

@model ReMod.CementType is basically saying that the Model you are referring to is of type ReMod.CementType. After declaring that, you can refer to that model using model. That is my understanding. try doing this:

@model ReMod.CementType

@Html.RadioButtonFor(model => model, model.R, new { @id = "CementTypeR" })
<label for="CementTypeR">R</label>
@Html.RadioButtonFor(model => model, model.N, new { @id = "CementTypeN" })
<label for="CementTypeN">N</label>
@Html.RadioButtonFor(model => model, model.S, new { @id = "CementTypeS" })
<label for="CementTypeS">S</label>
one stevy boi
  • 577
  • 6
  • 24
  • This code throw System.Web.HttpCompileException {"\\Areas\\Concrete\\Views\\RheoCalc\\EditorTemplates\\CementType.cshtml(4): error CS0103: The name 'model' does not exist in the current context"} – user3546486 Apr 17 '14 at 19:45
  • Thank you for your quick response. I tried to add referred settings to web.config, but the web fails just after start. I'm fairly new in asp.net (and hobby programmer). However, according to my opinion, my code was not wrong in case of "value" parameter (MSDN says: value=If this radio button is selected, the value of the radio button that is submitted when the form is posted). So ReMod.CementType.R etc. is right input. Lukas – user3546486 Apr 17 '14 at 21:29
0

So never mind, that it doesn't work. I made the helper method like this. I know, it is not ideal solution...

@helper EditorForCementType(CementType type)
{
    @Html.RadioButtonFor(model => type, CementType.R, new { @id = "CementTypeR" })
    <label for="CementTypeR">R</label>
    @Html.RadioButtonFor(model => type, CementType.N, new { @id = "CementTypeN" })
    <label for="CementTypeN">N</label>
    @Html.RadioButtonFor(model => type, CementType.S, new { @id = "CementTypeS" })
    <label for="CementTypeS">S</label>
}