[RESOLVED]How can i set Html.Editfor() according to the Model value

I want to change Html.Editfor() value according to the model value which comes.
At my model there is field about gender.It’s value M(Male) or F(Female) comes from database and i want to save it another table with full description.
If model.gender is F Html.Editfor must show Female
It must show Female or Mela instead of M or F.What is the way?


HI Archangell,

Can you please share your code?

@model EkolBebek.Models.Baby

    ViewBag.Title = "Create";

  <div class="form-group">
            @Html.LabelFor(model => model.Gender, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Gender)
                @Html.ValidationMessageFor(model => model.Gender)

Can we put a if statement according to the model.Gender

Hi Archangell,

Base on my test in MVC 4 and MVC 5 (HtmlAttribute), I find that we can’t specify the value if we are using
Html.EditFor() method.

I suggest that you could use Html.TextBoxFor() or custom HtmlHelper.

Best Regards


Leave a Reply