Question

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.

I have this at global.asax

   AutoMapper.Mapper.CreateMap<OApplyIDViewModel, OApply>();

This is ViewModel

 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; }
    }

This is at Controller

//GET

     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 => x.OAId == Id).FirstOrDefault();
    Mapper.Map<OApplyIDViewModel,OApply>(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");

}

This is the View

   @using (Html.BeginForm("ClientEditId", "OApply", FormMethod.Post, new { enctype = "multipart/form-data", @class = "stdform" }))
  {
    @Html.ValidationSummary(true)
      <fieldset>
    <legend>OnlineApplication</legend>
      @Html.HiddenFor(model => model.OAId)

    <div class="related">
    <div class="editor-label">
        @Html.LabelFor(model => model.IdId, "IdType")
    </div>
    <div class="editor-field">
        @Html.DropDownList("IdId", String.Empty)
        @Html.ValidationMessageFor(model => model.IdId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.AIdentificationNo)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AIdentificationNo)
        @Html.ValidationMessageFor(model => model.AIdentificationNo)
    </div>

    <div class="requiredfields">
    <div class="editor-label">
        @Html.LabelFor(model => model.ALicenceVersion)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ALicenceVersion)
        @Html.ValidationMessageFor(model => model.ALicenceVersion)
    </div>
    </div>
    <div class="country">
     <div class="editor-label">
        @Html.LabelFor(model => model.CountryId)
    </div>
    <div class="editor-field">
        @Html.DropDownList("CountryId")
        @Html.ValidationMessageFor(model => model.CountryId)
    </div>
    </div></div>
      <div class="editor-label">
        @Html.LabelFor(model => model.SigDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SigDate)
        @Html.ValidationMessageFor(model => model.SigDate)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
Was it helpful?

Solution

There is something wrong here:

    var onlineid = db.OApply.Where(x => x.OAId == Id).FirstOrDefault();
->  Mapper.Map<OApplyIDViewModel,OApply>(oapply);
    oapply.UDateBy = Membership.GetUser().ProviderUserKey.ToString();
    oapply.UDate = DateTime.Now;
    db.Entry(onlineid).State= EntityState.Modified;

The line with the arrow returns a new OApply instance. It does not update onlineid. In fact, it has no idea about onlineid. Try the following.

Mapper.Map<OApplyIDViewModel,OApply>(oapply, onlineid);

Now it will modify onlineid instead of returning one. However, you should ignore the mapping for the primary key if it is an identity (auto-incrementing) one.

AutoMapper.Mapper.CreateMap<OApplyIDViewModel, OApply>()
    .ForMember(dest => dest.OAId, opt => opt.Ignore());

I am not sure if OAId is your primary key or not. You are not following naming conventions and probably some other conventions too, at all.


I have made corrections in your code :

public ActionResult  ClientEditID(int id)
{
 OApply oapply  = db.OApply.Find(id);

->if (oapply == null )
 {
     return HttpNotFound();
 }
->var model = Mapper.Map<OApply, OApplyIDViewModel>(oapply); 

 ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName", model.CountryId);
 ViewBag.IdId = new SelectList(db.PhotoIds, "IdId", "IdName",  model.IdId);

->return View(model);
  } 

Your HttpPost is mostly valid, except that you put data into ViewBag before you use RedirectToAction(). That data will be lost. Instead, use TempData dictionary. Check msdn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top