Asp.net mvc 4 How to get default selection into selectlist MVC

Asp.net mvc 4 How to get default selection into selectlist MVC,asp.net-mvc-4,razor-2,selectlist,dropdownlistfor,Asp.net Mvc 4,Razor 2,Selectlist,Dropdownlistfor,I need to use dropdownlist into razor and related object into model object. I want to keep default selected value for the object but it did not work out. Can any one suggest me what should be the best way to achieve this - what to keep into DTO property and controller action and razor syntax. Below is my current stuff: public class StateDTO { public int ID { get; set; } public string Name { get; set; } } public class RegisterModel { [Required]

I need to use dropdownlist into razor and related object into model object.

I want to keep default selected value for the object but it did not work out.

Can any one suggest me what should be the best way to achieve this - what to keep into DTO property and controller action and razor syntax.

Below is my current stuff:

 public class StateDTO
  {
    public int ID { get; set; }
    public string Name { get; set; }

  }


     public class RegisterModel
           {
           [Required]
    [Display(Name = "User name", Order = 2)]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password", Order = 3)]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password", Order = 4)]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Display(Name = "Email Address", Order = 1)]
    [DataType(DataType.EmailAddress)]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Please enter value")]
    public string Email { get; set; }

    public IEnumerable<SelectListItem> States { get; set; }

    public SelectListItem State { get; set; }

}


      public ActionResult Register()
     {

        RegisterModel r = new RegisterModel();
        IList<StateDTO> s = new List<StateDTO>();
        s.Add(new StateDTO() { ID = 1, Name = "CA" });
        s.Add(new StateDTO() { ID = 2, Name = "LA" });
        s.Add(new StateDTO() { ID = 3, Name = "AR" });
        r.State = new SelectListItem() { Text = s[2].Name, Value = s[2].ID.ToString(), Selected = true };
        r.States = new SelectList(s, "ID", "Name",r.State);
        return View(r);
      }

View contains below related stuff:

    <div class="editor-label">
        @Html.LabelFor(model => model.State)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.State, Model.States)
        @Html.ValidationMessageFor(model => model.State)
    </div>

Please suggest me what should I change into Model object, controller and razor view to achieve default selection of dropdown list... any best way.

Thank You


#1

I have modified the stuff as follow the below link and it worked out.

dropdownlist set selected value in MVC3 Razor

In model, changed the property type as follow:

    public IEnumerable<SelectListItem> States { get; set; }
    public int State { get; set; }

Controller action:

       IList<StateDTO> s = new List<StateDTO>();
        s.Add(new StateDTO() { ID = 1, Name = "CA" });
        s.Add(new StateDTO() { ID = 2, Name = "LA" });
        s.Add(new StateDTO() { ID = 3, Name = "AR" });
        r.State = 3;
        r.States = new SelectList(s, "ID", "Name",3);

View is as follow:

    <div class="editor-label">
            @Html.LabelFor(model => model.State)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.State, Model.States)
            @Html.ValidationMessageFor(model => model.State)
        </div>

It works...got default selection.

Thank you