Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping of ViewModel with SelectList in MVC3
    primarykey
    data
    text
    <p>Hi I'm struggling to find the correct approach on SO for what I am currently doing, so I thought I would ask.</p> <p>Here is my simplified code:</p> <p>The entities are nested types based on using them with EF CodeFirst and the ViewModel is being mapped with AutoMapper.</p> <p>When posting the form the ModelState is not valid due to the dropdownlist being mapped to model.CourseId and displaying my Course data.. i.e. CourseId = 2, CourseList = Null, but also having the [Required] attribute, really only CourseId is required but I also needed a relevant error message.</p> <p>I then thought that in my Create GET &amp; POST actions the view should probably just have the CourseId but I still need to display it as a dropdown and populate it and I was unsure as how to do that correctly. </p> <p>I may also not be understanding how this should be used correctly and if I even need CourseName, i.e. since the Course already exists in the database I just want a foreign key to it, which will still let me show the selected course.</p> <p>I'm also planning to break out all this mapping and data setting in my controller actions into a separate service layer but at the moment its a small prototype.</p> <pre><code>// Entities public class Recipe { public int Id { get; set; } public string Name { get; set; } public Course Course { get; set; } } public class Course { public int Id { get; set; } public string Name { get; set; } } // View Model public class RecipeCreateViewModel { // Recipe properties public int Id { get; set; } public string Name { get; set; } // Course properties, as primitives via AutoMapper public int CourseId { get; set; } public string CourseName { get; set; } // For a drop down list of courses [Required(ErrorMessage = "Please select a Course.")] public SelectList CourseList { get; set; } } // Part of my View @model EatRateShare.WebUI.ViewModels.RecipeCreateViewModel ... &lt;div class="editor-label"&gt; Course &lt;/div&gt; &lt;div class="editor-field"&gt; @* The first param for DropDownListFor will make sure the relevant property is selected *@ @Html.DropDownListFor(model =&gt; model.CourseId, Model.CourseList, "Choose...") @Html.ValidationMessageFor(model =&gt; model.CourseId) &lt;/div&gt; ... // Controller actions public ActionResult Create() { // map the Recipe to its View Model var recipeCreateViewModel = Mapper.Map&lt;Recipe, RecipeCreateViewModel&gt;(new Recipe()); recipeCreateViewModel.CourseList = new SelectList(courseRepository.All, "Id", "Name"); return View(recipeCreateViewModel); } [HttpPost] public ActionResult Create(RecipeCreateViewModel recipe) { if (ModelState.IsValid) { var recipeEntity = Mapper.Map&lt;RecipeCreateViewModel, Recipe&gt;(recipe); recipeRepository.InsertOrUpdate(recipeEntity); recipeRepository.Save(); return RedirectToAction("Index"); } else { recipe.CourseList = new SelectList(courseRepository.All, "Id", "Name"); return View(recipe); } } </code></pre>
    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. 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