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 ...

有帮助吗?

解决方案 2

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>
}

其他提示

@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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top