使用实体框架更新模型-传递httpPost参数的空异常

本文关键字:参数 httpPost 异常 传递 实体 框架 更新 模型 | 更新日期: 2023-09-27 18:03:40

我正在学习MVC,我试图创建一个简单的表单,允许用户更新模型的描述。

问题是我得到一个空异常

parameters字典包含一个空的parameter条目非空类型的'ThreatID'。Int32' for方法'System.Web.Mvc.ActionResult GetThreat(Int32)' in"RiskAssesmentApplication.Controllers.ThreatsController"。一个可选的参数必须是引用类型、可空类型,或者声明为可选参数。参数名称: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; }

我真的被这个难住了,所以任何帮助都会很感激

使用实体框架更新模型-传递httpPost参数的空异常

您的GetThreat()方法期望参数int ThreatID(不可为空),但在您的EditThreat()方法中,您在重定向时不传递值。把

return RedirectToAction("GetThreat");

return RedirectToAction("GetThreat", new { ThreatID = Threat.ID });

没有看到你的代码的其余部分,很难说,但它听起来像你正在与一个断开连接的实体工作。仅仅因为您正在使用具有数据库中行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();   
}

RedirectToAction替换为:

return RedirectToAction("EditThreat", new { ThreatID = 99 });