Asp.net mvc 4 使用Automapper编辑模型MVC4的选定字段
我正在尝试使用原始模型(OApply)的viewmodel(OApplyIDViewModel)更新所选字段。运行时,更改不会生效。我将感谢任何有这方面经验的人的帮助。我没有得到任何错误。表单提交并重定向 我在global.asax有这个Asp.net mvc 4 使用Automapper编辑模型MVC4的选定字段,asp.net-mvc-4,automapper,asp.net-mvc-viewmodel,Asp.net Mvc 4,Automapper,Asp.net Mvc Viewmodel,我正在尝试使用原始模型(OApply)的viewmodel(OApplyIDViewModel)更新所选字段。运行时,更改不会生效。我将感谢任何有这方面经验的人的帮助。我没有得到任何错误。表单提交并重定向 我在global.asax有这个 AutoMapper.Mapper.CreateMap<OApplyIDViewModel, OApply>(); 这是控制器 //得到 public-ActionResult-ClientEditID(int-id) { var模型=新的
AutoMapper.Mapper.CreateMap<OApplyIDViewModel, OApply>();
这是控制器
//得到
public-ActionResult-ClientEditID(int-id)
{
var模型=新的OApplyIDViewModel();
OApply OApply=db.OApply.Find(id);
if(model==null)
{
返回HttpNotFound();
}
ViewBag.CountryId=新的选择列表(db.Countries,“CountryId”,“CountryName”,model.CountryId);
ViewBag.IdId=新的选择列表(db.PhotoIds,“IdId”,“IdName”,model.IdId);
返回视图();
}
[HttpPost]
public ActionResult客户端编辑Id(OApplyIDViewModel oapply,int-Id)
{
如果(!ModelState.IsValid)
{
返回视图(oapply);
}
var onlineid=db.OApply.Where(x=>x.OAId==Id).FirstOrDefault();
Mapper.Map(oapply);
oapply.UDateBy=Membership.GetUser().ProviderUserKey.ToString();
oapply.UDate=DateTime.Now;
db.Entry(onlineid).State=EntityState.Modified;
db.SaveChanges();
ViewBag.CountryId=新的选择列表(db.Countries,“CountryId”,“CountryName”,oapply.CountryId);
ViewBag.IdId=新的选择列表(db.PhotoIds,“IdId”,“IdName”,oapply.IdId);
返回重定向操作(“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>
@使用(Html.BeginForm(“clientediid”、“OApply”、FormMethod.Post、new{enctype=“multipart/form data”、@class=“stdform”}))
{
@Html.ValidationSummary(true)
在线应用
@Html.HiddenFor(model=>model.OAId)
@LabelFor(model=>model.IdId,“IdType”)
@DropDownList(“IdId”,String.Empty)
@Html.ValidationMessageFor(model=>model.IdId)
@LabelFor(model=>model.AIdentificationNo)
@EditorFor(model=>model.AIdentificationNo)
@Html.ValidationMessageFor(model=>model.AIdentificationNo)
@LabelFor(model=>model.ALicenceVersion)
@EditorFor(model=>model.ALicenceVersion)
@Html.ValidationMessageFor(model=>model.ALicenceVersion)
@LabelFor(model=>model.CountryId)
@Html.DropDownList(“CountryId”)
@Html.ValidationMessageFor(model=>model.CountryId)
@LabelFor(model=>model.SigDate)
@EditorFor(model=>model.SigDate)
@Html.ValidationMessageFor(model=>model.SigDate)
这里有点问题:
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;
现在它将修改onlineid
,而不是返回一个。但是,如果主键是identity
(自动递增)主键,则应该忽略它的映射
您的
HttpPost
基本上是有效的,除了在使用RedirectToAction()之前将数据放入ViewBag
。这些数据将丢失。相反,请使用TempData
字典。检查msdn。非常感谢。我已经实施了您的建议,并在用其他内容清除代码后。访问该视图时,我会得到一个空的get视图。如果您能帮助Thankstanks@mostruash查看,我已经更新了代码以包含get视图现在已填充,但我在提交时遇到错误。键为“CountryId”的ViewData项的类型为“System.Int32”,但必须为“IEnumerable”。@Diin check
@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>
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;
Mapper.Map<OApplyIDViewModel,OApply>(oapply, onlineid);
AutoMapper.Mapper.CreateMap<OApplyIDViewModel, OApply>()
.ForMember(dest => dest.OAId, opt => opt.Ignore());
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);
}