Note that there are some explanatory texts on larger screens.

plurals
  1. POdropdown value null when using, viewmodel & modelbinder in asp.net mvc
    text
    copied!<p>I am using asp.net's modelbinder functionality to bind form values to my entity when posting from a view. </p> <p>The html renders correctly in the initial view with correct option and value items.</p> <p>When completing the form and posting, all values are populated correctly into the entity except the value from the dropdown list. not sure what I am doing wrong.</p> <p>code attached below:</p> <p>Customer Entity:</p> <pre><code> public class Customer : EntityBase { public virtual string Name { get; set; } public virtual string Email { get; set; } public virtual string Mobile { get; set; } public virtual Store LocalStore { get; set; } public virtual DateTime? DateOfBirth { get; set; } public Customer(){} public Customer(string name, string email, string mobile, Store localStore):this(name, email, mobile, localStore, null) { } public Customer(string name, string email, string mobile, Store localStore, DateTime? dateOfBirth) { Name = name; Email = email; Mobile = mobile; LocalStore = localStore; DateOfBirth = dateOfBirth; } } </code></pre> <p>ViewModel:</p> <pre><code> public class CustomerViewModel { // Properties private IStoreRepository _StoreRepository; public Customer Customer { get; private set; } public SelectList Stores { get; private set; } // Constructor public CustomerViewModel(IStoreRepository storeRepository, Customer customer) { _StoreRepository = storeRepository; Customer = customer; Stores = new SelectList(_StoreRepository.GetAllStores(), "Id", "Name", Customer.LocalStore.Id); } } </code></pre> <p>Controller:</p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Prefix="")]Customer customer) { return View(new CustomerViewModel(_StoreRepository, customer)); } </code></pre> <p>View:</p> <pre><code>&lt;%@ Import Namespace="BlackDiamond.Buzz.MVCWeb.Controllers"%&gt; &lt;%@ Import Namespace="BlackDiamond.Buzz.Core"%&gt; &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;CustomerViewModel&gt;" %&gt; &lt;% Customer customer = ViewData.Model.Customer; using (Html.BeginForm()) { %&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Local Store:&lt;/td&gt; &lt;td&gt;&lt;%= Html.DropDownList("LocalStore", ViewData.Model.Stores)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Name:&lt;/td&gt;&lt;td&gt;&lt;%= Html.TextBox("Name", customer.Name)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Email:&lt;/td&gt;&lt;td&gt;&lt;%= Html.TextBox("Email", customer.Email)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Mobile:&lt;/td&gt;&lt;td&gt;&lt;%= Html.TextBox("Mobile", customer.Mobile)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="submit" value="Create" /&gt; &lt;%}%&gt; </code></pre>
 

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