Reputation: 2107
Using MVC 4, Razor
I have a model like this:
public class Device
{
[Required]
public string TYPE {get; set;}
}
I need to get the value of TYPE from user in a form, but they are limited to 3 values, "A" , "B" , "C"
How do i enforce/do this in the View section?
Right now I have:
@Html.EditorFor(model => model.TYPE)
but this will allow the user to write in anything they want
Upvotes: 0
Views: 112
Reputation: 176
The easiest way
@Html.DropDownListFor(model => model.TYPE,
new List<SelectListItem>
{
new SelectListItem { Text = "A", Value = "A" },
new SelectListItem { Text = "B", Value = "B" },
new SelectListItem { Text = "C", Value = "C" },
})
Upvotes: 1
Reputation: 1870
You are better off using a DropDownListFor
and letting the user select from the available values. First create an enum for TYPE:
public enum PickType
{
"A",
"B",
"C",
}
Then in your View:
@Html.DropDownListFor(model=>model.TYPE, new SelectList(Enum.GetValues(typeof(PickType))))
Upvotes: 0