Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Automapper to Edit selected fields of a model MVC4
    text
    copied!<p>I am trying to update selected fields using a viewmodel(OApplyIDViewModel) of the original model(OApply). When I run changes are not effected. I will appreciate help from anyone who is experienced with this. I do not get any error. The form submits and redirects.</p> <p>I have this at global.asax</p> <pre><code> AutoMapper.Mapper.CreateMap&lt;OApplyIDViewModel, OApply&gt;(); </code></pre> <p>This is ViewModel</p> <pre><code> public class OApplyIDViewModel { [Key] public int OAId { get; set; } [Display(Name = "Identification")] [Required(ErrorMessage = "Identification Required")] public int IdId { get; set; } [Display(Name = "Identification Number")][Required(ErrorMessage="ID Number Required")] public string AIdentificationNo { get; set; } [Display(Name = "Licence Version(5b)")] [RequiredIf("IdId", Comparison.IsEqualTo, 1, ErrorMessage = "Version(5b) Required")] public string ALicenceVersion { get; set; } public int CountryId { get; set; } [RequiredIf("IdId",Comparison.IsNotEqualTo,1, ErrorMessage="Country Required")] [Display(Name = "Your Electronic Signature Date Seal")] [Required(ErrorMessage="Date Signature Seal Required")] public DateTime SigDate { get; set; } [ScaffoldColumn(false)] [Display(Name = "Last Updated on")] public DateTime UDate { get; set; } [ScaffoldColumn(false)] [Display(Name = "Last Updated by")] public String UDateBy { get; set; } } </code></pre> <p>This is at Controller</p> <p>//GET</p> <pre><code> public ActionResult ClientEditID(int id) { var model = new OApplyIDViewModel(); OApply oapply = db.OApply.Find(id); if (model == null ) { return HttpNotFound(); } ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName", model.CountryId); ViewBag.IdId = new SelectList(db.PhotoIds, "IdId", "IdName", model.IdId); return View(); } [HttpPost] public ActionResult ClientEditId(OApplyIDViewModel oapply, int Id) { if (!ModelState.IsValid) { return View(oapply); } var onlineid = db.OApply.Where(x =&gt; x.OAId == Id).FirstOrDefault(); Mapper.Map&lt;OApplyIDViewModel,OApply&gt;(oapply); oapply.UDateBy = Membership.GetUser().ProviderUserKey.ToString(); oapply.UDate = DateTime.Now; db.Entry(onlineid).State= EntityState.Modified; db.SaveChanges(); ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName", oapply.CountryId); ViewBag.IdId = new SelectList(db.PhotoIds, "IdId", "IdName", oapply.IdId); return RedirectToAction("HomeAccount"); </code></pre> <p>}</p> <p>This is the View</p> <pre><code> @using (Html.BeginForm("ClientEditId", "OApply", FormMethod.Post, new { enctype = "multipart/form-data", @class = "stdform" })) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;OnlineApplication&lt;/legend&gt; @Html.HiddenFor(model =&gt; model.OAId) &lt;div class="related"&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.IdId, "IdType") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("IdId", String.Empty) @Html.ValidationMessageFor(model =&gt; model.IdId) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.AIdentificationNo) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.AIdentificationNo) @Html.ValidationMessageFor(model =&gt; model.AIdentificationNo) &lt;/div&gt; &lt;div class="requiredfields"&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.ALicenceVersion) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.ALicenceVersion) @Html.ValidationMessageFor(model =&gt; model.ALicenceVersion) &lt;/div&gt; &lt;/div&gt; &lt;div class="country"&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.CountryId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("CountryId") @Html.ValidationMessageFor(model =&gt; model.CountryId) &lt;/div&gt; &lt;/div&gt;&lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.SigDate) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.SigDate) @Html.ValidationMessageFor(model =&gt; model.SigDate) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&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