2016-02-29 122 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"); 
    } 

见证是输入框,这是相同的名称作为数据库表中的条目的名称。

相关问题