Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you have a <code>PersonsProduct</code> class to represent your model/viewmodel like this</p> <pre><code>public class PersonsProduct { public string PersonName { set;get;} public IEnumerable&lt;SelectListItem&gt; Products { get; set; } public string SelectedProductId { get; set; } //Other Properties } </code></pre> <p>In your <code>GET</code> action method, you may initialize the values like this</p> <pre><code>public ActionResult AddProduct() { var objProduct = new PersonsProduct(); objProduct.Products = new[] { new SelectListItem { Value = "1", Text = "Book" }, new SelectListItem { Value = "2", Text = "Pen" }, new SelectListItem { Value = "3", Text = "Computer" }, new SelectListItem { Value = "4", Text = "Table" }, new SelectListItem { Value = "5", Text = "Mouse" } }; // can replace the above line with loading data from Data access layer. return View(objProduct); } </code></pre> <p>And in your View which is strongly typed to <code>PersonsProduct</code> viewmodel</p> <pre><code>@model PersonsProduct @using (Html.BeginForm()) { @Html.DropDownListFor(x =&gt; x.SelectedProductId, new SelectList(Model.Products ,"Value","Text"), "Select..") &lt;input type="submit" value="save" /&gt; } </code></pre> <p>And in your <code>HTTPPost</code> action, you can get the selected productId like this</p> <pre><code>[HttpPost] public ActionResult Index(PersonsProduct model) { // check model.SelectedProductId here } </code></pre> <h2>NEVER HARDCODE LIKE THIS. It is UGLY UGLY UGLY !</h2> <p>This answers your question (without using Database and XML). <strong>But I strongly recommend to avoid this hardcoding approach</strong>. You should better use different source for the list of products like database / XML file etc.. Otherwise whenever you want to add a new product to the list, you need a recompilation. I hope you will change this code to read Product data from a different source instead of hard coded values.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload