0

I have interface:

public interface IUserVerificationDocument
{

    UserDocumentType DocumentType { get; set; }

    void Save();
}

and i have enum:

public enum UserDocumentType { IdCard, Passport, DrivingLicense, ResidentsPermit }

how can i set value for this UserDocumentType with dropdown list or something like that?

ekad
  • 14,436
  • 26
  • 44
  • 46
None
  • 8,817
  • 26
  • 96
  • 171
  • this may help: http://stackoverflow.com/questions/28807472/get-enum-value-to-show-on-dropdownlist-asp-net-mvc – Ehsan Sajjad May 28 '15 at 11:58
  • possible duplicate of [MVC3 Razor DropDownListFor Enums](http://stackoverflow.com/questions/4656758/mvc3-razor-dropdownlistfor-enums) – Luke May 28 '15 at 12:08

1 Answers1

0

There are many solutions out there. You will find many on your very first search. But here is a simple solution that I have used in past. Maybe it can help you. What I am going to do is show dropdownlist from Enum and back to binding enum on bind model as selected.

Models:

public enum UserDocumentType
{
    IdCard,
    Passport,
    DrivingLicense,
    ResidentsPermit
}

public class SampleViewModel
{
    public UserDocumentType DocType
    {
        get;
        set;
    }
}

Controller:

public ActionResult Index()
{
    //Select default as Passport
    var user = new SampleViewModel
    {
        DocType = UserDocumentType.Passport
    };
    return View(user);
}

public void Submit(SampleViewModel vm)
{
    //vm.DocType would be what was selected when submit was clicked
}

View:

    <form action="@Url.Action("Submit")" method="post">
        @Html.DropDownList("DocType", 
        Enum.GetNames(typeof(HelloWorldMvcApp.UserDocumentType)).Select(n => new SelectListItem { Text = n, Value = n, Selected = n == Enum.GetName(typeof(HelloWorldMvcApp.UserDocumentType), Model.DocType) }))
        <input type="submit" name="Submit" value="Submit" />
    </form>

Here is an executable you can run or copy into your solution and try out: https://dotnetfiddle.net/DXCcHp

lbrahim
  • 3,710
  • 12
  • 57
  • 95