0

this is my Model1 class

namespace chetan.Models
{
  public  class Model1
    {
        public string selectedItem { get; set; }
        public IEnumerable<SelectListItem> 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 SelectListItem { Value = "Theory", Text = "Theory" },
            new SelectListItem { Value = "Appliance", Text = "Appliance" },
            new SelectListItem { Value = "Lab", Text = "Lab" }
        }
        }; return View(model);

    }

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


}

this is my view...

@using (Html.BeginForm("viewToController", "Home"))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>emp</legend>

    <div class="editor-field">

        @Html.DropDownListFor(x => x.selectedItem, 
            new SelectList(Model.items, "Value", "Text"))

    </div>
<p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

i want to add a drop downlist and i want to use selected value in viewToController action of homeController. and there is also one error in View page is "an expression tree may not contain dynamic operation" in (x=>x.selectedItem). Please solve my problem .

Khepri
  • 9,547
  • 5
  • 45
  • 61
Pushpendra Kuntal
  • 6,118
  • 20
  • 69
  • 119
  • 1
    have you declared the model type in your view like `@model Model1`? – Eranga Jun 13 '11 at 02:15
  • Eranga, I appreciate your answer,my problem has resolved. Please suggest me what should i do if i want to add items in dropdown list using databade. my database Entity is rikuEntities re=new rikuEntities(); and my table name is employee. so please suggest me what should i do to access value in drop downlist from database. – Pushpendra Kuntal Jun 13 '11 at 02:57

1 Answers1

-1

I don't understnad what you exactly need. You want to dynamicly add items to the drop down from the database?

I'm big fan of jQuery. You can do everything what you want with HTML using jQuery. So if you are looking how to automaticly add items to the drop down, take look at this: How do I add options to a DropDownList using jQuery?

Community
  • 1
  • 1
Dawid Rutkowski
  • 2,658
  • 1
  • 29
  • 36