Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net-MVC-3 Beginner problems with ViewModels and submitting them back to the controller
    text
    copied!<p>I have build my entities via database-first, since I have to use an existing db.</p> <p>Now, let's say I've got the entity customer and country and I want to edit the customer. My View should contain something like First Name, Last Name and Country which is a DropdownList from all countries in the entity. For that I created a CustomerViewModel which uses these two entities. But I dont' know if all of this is right, since some things don't work.</p> <p>First the code of my CustomerViewModel:</p> <pre><code>public class CustomerViewModel { public CUSTOMER customer { get; set; } public COUNTRY country { get; set; } public IEnumerable&lt;SelectListItem&gt; countryList { get; set; } MyEFEntities db = new MyEFEntities(); public CustomerViewModel(int id) { IEnumerable&lt;CUSTOMER&gt; customerList = from c in db.CUSTOMER where c.CUSTOMERNO== id select c; customer = customerList.First(); var countryListTemp = new List&lt;String&gt;(); var countryListQry = from s in db.COUNTRY select s.COUNTRY_ABBREVIATION; countryListTemp.AddRange(countryListQry); countryList = new SelectList(countryListTemp); } } </code></pre> <p>Then the CustomerController.cs:</p> <pre><code>public ViewResult CustomerData(int id = 0) { // if (id == 0) { ... } var viewModel = new CustomerViewModel(id); return View(viewModel); } [HttpPost] public ActionResult CustomerData(CustomerViewModel model) { db.Entry(model.customer).State = System.Data.EntityState.Modified; db.SaveChanges(); return View(model); } </code></pre> <p>And last but not least the CustomerData.cshtml:</p> <pre><code>@model MyApp.ViewModels.CustomerViewModel @{ ViewBag.Title = "Customer"; } &lt;h2&gt; Customer&lt;/h2&gt; @using (Html.BeginForm("CustomerData", "Customer")) { @Html.ValidationSummary(true) &lt;div id="tabs"&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#data"&gt;Data&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#hobbies"&gt;Hobbies&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#stuff"&gt;Stuff&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;div id="data"&gt; &lt;div class="editor-label"&gt; @Html.Encode("Country:") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(m =&gt; m.country.COUNTRY_ABBREVIATION, Model.countryList, Model.customer.ADDRESS.COUNTRY.COUNTRY_ABBREVIATION) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.Encode("Last name:") @Html.TextBox("lastName", @Model.customer.ADDRESS.NAME) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.Encode("First Name:") @Html.TextBox("firstName", @Model.customer.ADDRESS.FIRSTNAME) &lt;/div&gt; &lt;/div&gt; &lt;div id="hobbies"&gt; &lt;p&gt; Hobbies &lt;/p&gt; &lt;/div&gt; &lt;div id="stuff"&gt; &lt;p&gt; Stuff &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; } </code></pre> <p>Viewing works great. I get to the URL <a href="http://localhost:12345/Customer/CustomerData/4711" rel="nofollow">http://localhost:12345/Customer/CustomerData/4711</a> and can see the current values for that customer.</p> <p>Now, I'm missing some stuff.</p> <ol> <li><p>The Save-button doesn't work. If I click it, I got an error message, that no parameterless constructor was found for this object. But I want to pass the ViewModel back to the controller. How to overload the Html.BeginForm() method?!</p></li> <li><p>How to I store the changed values from the customer? Is is done by editing the text-fields or do I have to use Html.TextboxFor() instead of Html.Textbox? This is so complicated for a beginner. I'm not into the LINQ style at all.</p></li> <li><p>The Dropdownlist doesn't work as supposed to. The country the customer has already is twice in it. It seems, that the third parameter does not preselct an item but add a default one. Would Html.Dropdownlist be better?</p></li> </ol>
 

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