0

我正在尝试使用原始模型(OApply)的视图模型(OApplyIDViewModel)更新选定的字段。当我运行更改时不会生效。我将感谢任何对此有经验的人的帮助。我没有收到任何错误。表单提交和重定向。

我在 global.asax 有这个

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

这是视图模型

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

这是在控制器

//得到

     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");

}

这是视图

   @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>
4

1 回答 1

0

这里有问题:

    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;

带箭头的行返回一个新OApply实例。它不更新onlineid。事实上,它对onlineid. 试试下面的。

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

现在它将修改onlineid而不是返回一个。但是,如果主键是identity(自动递增的)映射,您应该忽略它。

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

我不确定是否OAId是您的主键。您根本没有遵循命名约定,可能也没有遵循其他一些约定。


我已在您的代码中进行了更正:

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

你的大部分是有效的,除了你在使用之前HttpPost将数据放入。该数据将丢失。相反,使用字典。检查msdn。ViewBagRedirectToAction()TempData

于 2013-07-24T22:17:52.047 回答