我正在尝试使用原始模型(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>