2016-02-29 68 views
0

我有以下幾點看法(基本的 「編輯」 模板)值不能爲空。參數名:實體

@model SuccessStories.Models.Testimonials 
 

 
@using (Html.BeginForm()) 
 
{ 
 
    @Html.AntiForgeryToken() 
 
    
 
    <div class="form-horizontal"> 
 
     <h4>Testimonials</h4> 
 
     <hr /> 
 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
 
     @Html.HiddenFor(model => model.Id) 
 

 
     <div class="form-group"> 
 
      @Html.LabelFor(model => model.Testimonial, htmlAttributes: new { @class = "control-label col-md-2" }) 
 
      <div class="col-md-10"> 
 
       @Html.EditorFor(model => model.Testimonial, new { htmlAttributes = new { @class = "form-control" } }) 
 
       @Html.ValidationMessageFor(model => model.Testimonial, "", new { @class = "text-danger" }) 
 
      </div> 
 
     </div> 
 

 
     <div class="form-group"> 
 
      <div class="col-md-offset-2 col-md-10"> 
 
       <input type="submit" value="Save" class="btn btn-default" /> 
 
      </div> 
 
     </div> 
 
    </div> 
 
} 
 

 
<div> 
 
    @Html.ActionLink("Back to List", "Index") 
 
</div>

而在我的控制下的ActionResult:

[HttpGet] 
    public ActionResult Edit(int id) 
    { 
      TestimonialsContext testContext = new TestimonialsContext(); 
      Testimonials testimonials = testContext.testimonialContext.Find(id); 
      return View(testimonials); 
    } 

    [HttpPost] 
    public ActionResult Edit(Testimonials testimonial) 
    { 
     TestimonialsContext testContext = new TestimonialsContext(); 
     testContext.Entry(testimonial).State = EntityState.Modified; 
     testContext.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 

的錯誤是在此行:

testContext.Entry(testimonial).State = EntityState.Modified; 

我得到的錯誤是「值不能爲空。 參數名稱:實體

描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.ArgumentNullException:值不能爲空。 參數名:。!。實體」

請幫我看這件事,但無法找到一個解決方案,將工作給我看

+0

可能的是,你的'的TestContext。條目(推薦)''爲'null'或'推薦'爲'空' – Ian

+0

我的數據庫表只有一個id和證書類別。證詞編輯正在文本輸入中捕獲。問題可能是它將id更改爲null? –

+1

'testimonial'爲null。 – Rob

回答

0

感謝您的幫助大家我這樣想通了,以解決這個問題的基礎上,你告訴我什麼是零。

 [HttpPost, ActionName("Edit")] 
    public ActionResult EditConfirmed(int id, string Testimonial) 

    { 
     TestimonialsContext testContext = new TestimonialsContext(); 
     Testimonials testimonial = testContext.testimonialContext.Find(id); 
     testimonial.Testimonial = Testimonial; 
     testContext.Entry(testimonial).State = EntityState.Modified; 
     testContext.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

見證是輸入框,這是相同的名稱作爲數據庫表中的條目的名稱。

相關問題