提问人:Erick Bongo 提问时间:7/20/2015 最后编辑:Erick Bongo 更新时间:7/23/2015 访问量:193
MVC4 - 使用实体框架更新模型 - 传递 httpPost 参数的 Null 异常
MVC4 - Updating Model using Entity Framework - Null Exception passing httpPost paramater
问:
我正在学习 MVC 并尝试创建一个简单的表单,该表单将允许用户更新模型的描述。
问题是我得到一个空异常
parameters 字典包含参数的 null 条目 方法的不可为 null 类型“System.Int32”的“ThreatID” “System.Web.Mvc.ActionResult GetThreat(Int32)”中的 “RiskAssesmentApplication.Controllers.ThreatsController”。可选的 参数必须是引用类型、可为 null 的类型,或者声明为 可选参数。参数名称:parameters
表单的 Get 方法似乎按预期工作,但 id 没有传递回 HttpPost 方法参数,我无法工作应该如何传递它。我进行了搜索并看到了有关使用助手的一些内容,但它对我不起作用。@hiddenfor
这是我的方法
public ActionResult GetThreat(int ThreatID)
{
// ViewModel.Threat = repository.GetThreat(ThreatID);
RiskAssessmentApplicationEntities _DBContext = new RiskAssessmentApplicationEntities();
ThreatWithSecurityEventAndISOControlViewModel ViewModel = new ThreatWithSecurityEventAndISOControlViewModel();
ViewModel.Threat = _DBContext.Threats.Single(x => x.ID == ThreatID);
ViewModel.SecurityEvents = _DBContext
.ThreatHasSecurityEvents
.Include("ThreatHasSecurityEvent.SecurityEvent")
.Where(x => x.ThreatID == ThreatID)
.Select(x => x.SecurityEvent);
return View(ViewModel);
}
[HttpGet]
public ViewResult EditThreat(int ThreatID)
{
Threat Threat = repository.Threats.FirstOrDefault(x => x.ID == ThreatID);
return View(Threat);
}
[HttpPost]
public ActionResult EditThreat(Threat Threat)
{
if (ModelState.IsValid)
{
repository.SaveThreat(Threat);
TempData["message"] = string.Format("{0} new description has been saved", Threat.Description);
return RedirectToAction("GetThreat");
}
else
{
// something is incorrect!
return View(Threat);
}
}
这是我的观点
@model RiskAssesmentApplication.Threat
@using RiskAssesmentApplication;
@{
ViewBag.Title = "EditThreat";
}
<div style="font-family: Calibri">
<fieldset>
<legend>Edit Threat Description</legend>
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.ID);
<div class="editor-label">
@Html.LabelFor(model => @Model.Description, "Threat Description")
</div>
<div class="editor-field">
@Html.EditorFor(model => @Model.Description)
@Html.ValidationMessageFor(model => @Model.Description )
</div>
<p>
<input type="submit" value="Save Changes" />
</p>
}
</fieldset>
</div>
这是我的模型
public class ThreatWithSecurityEventAndISOControlViewModel
{
public Threat Threat { get; set; }
public SecurityEvent SecurityEvent { get; set; }
public IEnumerable<ISOControl> ISOControls { get; set; }
public IEnumerable<SecurityEvent> SecurityEvents { get; set; }
我真的被难住了,所以任何帮助将不胜感激
答:
0赞
Stephen Brickner
7/20/2015
#1
如果没有看到代码的其余部分,很难确定,但听起来您正在使用一个断开连接的实体。仅仅因为您使用的实体模型在数据库中具有行的 ID,并不意味着它已连接到您的数据上下文。使用在帖子中传回的模型来查找连接的版本。例:
[HttpPost]
public void Whatever(Threat model)
{
var connectedModel = context.Threats.FirstOrDefault(x => x.ID == model.ID);
connectedModel.SomeProperty = model.SomeProperty; //or use AutoMapper
context.SaveChanges();
}
0赞
freethinker
7/20/2015
#2
替换为:RedirectToAction
return RedirectToAction("EditThreat", new { ThreatID = 99 });
2赞
user3559349
7/23/2015
#3
方法期望参数(不可为空),但在方法中,重定向时不传递值。改变GetThreat()
int ThreatID
EditThreat()
return RedirectToAction("GetThreat");
自
return RedirectToAction("GetThreat", new { ThreatID = Threat.ID });
评论
0赞
Erick Bongo
7/23/2015
谢谢,这正是它本来的样子。
评论
GetThreat()
EditThreat()
GetThreat()
int ThreatID
return RedirectToAction("GetThreat", new { ThreatID = Threat.ID });