2
votes

this is my model class public class Model1 { public string popcorn { get; set; } public string pselectedItem { get; set; } public IEnumerable items { get; set; } }

this is my controller class:-

public class HomeController : Controller
{
    private rikuEntities rk = new rikuEntities();
    public ActionResult Index()
    {
        var model = new Model1
        {
            items = new[]
        {
            new SelectList(rk.emp,"Id","name")

        }
        }; return View(model);
    }


    public ActionResult viewToController(Model1 m)
    {
        string getSelectedName = m.popcorn;
        return Content(getSelectedName);
    }


}

this is my view;-

@model chetan.Models.Model1 @using (Html.BeginForm("viewToController", "Home")) { @Html.ValidationSummary(true) emp

    <div class="editor-field">

        @Html.DropDownListFor(x => x.popcorn, new SelectList(Model.items))

    </div>

</fieldset>

}

in my example i want to get values in Dropdownlist from database table named "emp" and after select a element of dropdownlist i want to use that element in Index action of Home controller. please check my code carefully and let me know what should i do ?

1

1 Answers

1
votes

First in your model your model you need SelectList and not IEnumerable<SelectList>:

public class Model1
{
    public string popcorn { get; set; }
    public string pselectedItem { get; set; }
    public SelectList items { get; set; }
}

and then in your view:

@Html.DropDownListFor(x => x.popcorn, Model.items)

The rest of your code seems fine.