Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Jonathan stated, AutoMapper will help you map your ViewModel entities to your Domain model. Here is an example:</p> <p>In your view you work with the View Model (<strong>CreateGroupVM</strong>):</p> <pre><code>@model X.X.Areas.Group.Models.CreateGroupVM @using (Html.BeginForm(null,null, FormMethod.Post, new { @class="form-horizontal", role="form"})) { @Html.ValidationSummary() @Html.AntiForgeryToken() @Html.LabelFor(model =&gt; model.Title, new { @class = "col-lg-4 control-label" }) @Html.TextBoxFor(model =&gt; model.Title, new { @class = "form-control" }) @Html.ValidationMessageFor(model =&gt; model.Title) @Html.LabelFor(model =&gt; model.Description, new { @class = "col-lg-4 control-label" }) @Html.TextBoxFor(model =&gt; model.Description, new { @class = "form-control" }) @Html.ValidationMessageFor(model =&gt; model.Description) @Html.LabelFor(model =&gt; model.CategoryId, new { @class = "col-lg-4 control-label" }) @Html.DropDownListFor(x =&gt; x.CategoryId, Model.Categories) @Html.ValidationMessageFor(model =&gt; model.CategoryId) &lt;div class="form-group"&gt; &lt;div class="col-lg-offset-4 col-lg-8"&gt; &lt;button type="submit" class="btn-u btn-u-blue"&gt;Create&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; } </code></pre> <p>ViewModel (CreateGroupVM.cs): Notice how we pass in a list of Categories - you could not do this had you strictly used your domain model because you cant pass a list of categories in the Group model. This gives us strongly typed helpers in our views, and no ViewBag usage.</p> <pre><code>public class CreateGroupVM { [Required] public string Title { get; set; } public string Description { get; set; } [DisplayName("Category")] public int CategoryId { get; set; } public IEnumerable&lt;SelectListItem&gt; Categories { get; set; } } </code></pre> <p>Domain Model (Group.cs):</p> <pre><code>public class Group { public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } public int CategoryId { get; set; } public int CreatorUserId { get; set; } public bool Deleted { get; set; } } </code></pre> <p>In your HttpPost Create Action - you let AutoMapper do the mapping then save to the DB. Note that by default AutoMapper will map fields that are the same name. You can read <a href="https://github.com/AutoMapper/AutoMapper/wiki/Getting-started" rel="nofollow">https://github.com/AutoMapper/AutoMapper/wiki/Getting-started</a> to get started with AutoMapper.</p> <pre><code>[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(CreateGroupVM vm) { if (ModelState.IsValid) { var group = new InterestGroup(); Mapper.Map(vm, group); // Let AutoMapper do the work db.Groups.Add(group); db.SaveChanges(); return RedirectToAction("Index"); } return View(vm); } </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.
 

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