1

我查看了所有出现此错误的帖子,但没有任何帮助。我将 MVC 与 ASPX (C#) 中的视图一起使用。在简单视图中编辑数据后,我正在更新数据库中的记录。当我一次处理一条记录时(控制器编辑方法和编辑视图)一切正常。当我处理多个记录(控制器 UpdateTest 和 UPDATETEST 视图)时,单击视图中的提交按钮后,控制器中的 HTTPPOST 方法出现错误。

这是示例代码的一个错误,一个有效,一个无效:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:  (CONTROLLER – SEE BELOW HttpPost  UpdateTest(ICollection<Mysurvey> mysurveys)

enter code here

Line 48:         public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)
Line 49:         {
Line 50:             foreach (var survey in mysurveys)
Line 51:             {
Line 52:                 db.Entry(survey).State = EntityState.Modified;

Source File: C:\Users\rsc_vok\Documents\Visual Studio 2010   \Projects\MvcMysurvey\MvcMysurvey\Controllers\MysurveyController.cs    Line: 50

控制器

namespace MvcMysurvey.Controllers
{ 
 THE CODE WHICH WORKS – SINGLE EDIT
    public ActionResult Edit(int id)
    {
        Mysurvey mysurvey = db.Mysurveys.Find(id);
        return View(mysurvey);
    }

    // POST: /Mysurvey/Edit/5

    [HttpPost]
    public ActionResult Edit(Mysurvey mysurvey)
    {
        System.Diagnostics.Debug.WriteLine("iam in edit post");
        if (ModelState.IsValid)
        {
            db.Entry(mysurvey).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(mysurvey);
    }

THE CODE WITH THE ERROR IN httppost – MULTIPLE RECORDS SAVE


    public ActionResult UpdateTest(int id = 0)
    {

        List<Mysurvey> mysurveys = db.Mysurveys.ToList();

        return View(mysurveys.ToList());
    }

    [HttpPost]
    public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)
    {
        foreach (var survey in mysurveys)
        {
            db.Entry(survey).State = EntityState.Modified;
        }
        db.SaveChanges();
        return View(mysurveys);
    }

看法

UPDATETEST
THE CODE WHICH WORKS HERE BUT THROWS AN ERROR IN CONTROLLER AFTER THE SAVE BUTTON IS HIT

  <%@ Page Language="C#"  MasterPageFile="~/Views/Shared/Site.master"  Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcMysurvey.Models.Mysurvey>>" %>


<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Updatetest
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>"    type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

<% using (Html.BeginForm()) { %>
 <%: Html.ValidationSummary(true) %>
 <fieldset>
    <legend>Mysurvey</legend>
    <% foreach (var item in Model) {  %>
    <%: Html.HiddenFor(model => item.ID) %>
    <%: Html.HiddenFor(model => item.SurveyID) %>
    <div class="editor-label">
        <%: Html.LabelFor(model => item.Comment) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => item.Comment) %>
        <%: Html.ValidationMessageFor(model => item.Comment) %>
    </div>
    <%} %>
    <p>
        <input type="submit" value="Save" />
    </p>
  </fieldset>
 <% } %>

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

</asp:Content>

模型

namespace MvcMysurvey.Models
{
public class Mysurvey
{
    public int ID { get; set; }

    [DisplayFormat(DataFormatString = "{0:c}")] 

    public int SurveyID { get; set; }
    public string Comment { get; set; }
}
public class MysurveyDBContext : DbContext
{
    public DbSet<Mysurvey> Mysurveys { get; set; }
}
}
4

0 回答 0