0

我正在學習MVC,並試圖創建一個簡單的表單,允許用戶更新模型的描述。MVC4 - 使用實體框架更新模型 - 空異常傳遞httpPost參數

問題是我得到一個空的異常

參數字典包含參數 空條目方法 「System.Web.Mvc非可空類型「System.Int32」的「ThreatID」 .ActionResult GetThreat(Int32)' 'RiskAssesmentApplication.Controllers.ThreatsController'。可選 參數必須是引用類型,可爲空類型,或者聲明爲可選參數 。參數名稱:參數

窗體的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

該錯誤消息涉及的控制器方法'GetThreat()'但已示出的所有是'EditThreat()'方法 –

+0

假設'GetThreat()'具有參數'int ThreatID',那麼它需要像'return RedirectToAction(「GetThreat」,new {ThreatID = Threat.ID});' - 但你需要顯示你的方法和模型以確保 –

+0

嗨,謝謝你你的回覆,這個伎倆,我仍然會發布GetThreat()方法+模型以求清晰 –

回答

2

GetThreat()方法期望參數int ThreatID(不可爲空),但在你的EditThreat()方法,當你重定向你不及格的值。改變

return RedirectToAction("GetThreat"); 

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

謝謝,這正是它的原因。 –

0

With看到你的代碼的其餘部分很難說肯定,但聽起來你正在與一個斷開的實體工作。僅僅因爲你使用的數據庫中具有行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

替換您RedirectToAction由:

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