0

I am new to MVC 4, I have a view with a form. Many controls are there. One of them is simple Gender with @Html.DropDownListFor(). I have to send value to database and when clicked for edit need to show the same value from database in that dropdown.

I am not getting it, may be due to lac of knowledge. Please help me?

  • Thanks
Monibrata
  • 427
  • 4
  • 10
  • 25
  • Not clear what you are asking. How are your generating the dropdownlist. What is the property your bind to. You need to show some code. –  May 05 '15 at 05:12
  • I have no idea how to create and bind data...I am just asking how should I show everything...I haven't created any thing...other controls I have created but for this I am searching articles...like how to create a @Html.DropDownListFor() with "Male", "Female", and "Others"...How to send value to database and when it is needed to edit those values, the same data for this dropdown will show the value selected in the dropdown...thats it – Monibrata May 05 '15 at 05:24
  • 1
    You need a property in your model to bind to (e.g.`string Gender`) and a `SelectList` containing the values to display (e.g. var GenderList = new SelectList(new List { "Male", Female" };` and use `@HtlDropDownListFor(m => m.Gender, Model.GenderList)`. Go to the MVC site ad work through some basic tutorials. –  May 05 '15 at 05:29
  • Please refer this link. hope you can get some idea from this. http://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor – Jinesh Jain May 05 '15 at 05:39

1 Answers1

2

That is how we usually handle this task in our MVC projects:

@Html.DropDownListFor(x => x.Office.Id, (IEnumerable<SelectListItem>)ViewBag.Offices, new { @class = "form-control" })

Offices are represented as a IEnumerable<Nomenclature> where Nomenclature has Id and Name attributes

ViewBag.Offices = new SelectList(officesList, "Id", "Name")
noobed
  • 1,329
  • 11
  • 24