1

我对编程真的很陌生并且遇到了一个问题。

我正在尝试使用 mvc 和 asp.net 在一个视图中编辑和更新数据库的多行。我认为我在正确的轨道上的某个地方,但不断收到一条错误消息,说“并非所有代码路径都返回一个值”。我的控制器看起来像这样:

 [HttpGet]
    public ViewResult AnotherListEdit()
    {
        var chosenClass = from c in db.ClassInstanceDetails.Include("ClassInstance").Include("Student")
                          where c.ClassInstance.ID == 1
                          select c;

        return View(chosenClass.ToList());
    }


    [HttpPost]
    public ActionResult AnotherListEdit(IList<ClassInstanceDetail> list)
    {
        if (ModelState.IsValid)
        {
            foreach (ClassInstanceDetail editedClassInstanceDetail in list)
            {
                var tempBook = (from classInstDet in db.ClassInstanceDetails
                                where (teacher.ClassInstanceID == editedClassInstanceDetail.ClassInstanceID)
                                && (classInstDet.StudentID == editedClassInstanceDetail.StudentID)
                                select teacher).First();

                db.ApplyCurrentValues(tempBook.EntityKey.EntitySetName, editedClassInstanceDetail);
            }
            db.SaveChanges();
            return View(db.Teachers.ToList());
        }


    }

我的视图如下所示:

    @model IList<FYPSchoolApp.DAL.ClassInstanceDetail>

@{
    ViewBag.Title = "AnotherListEdit";
}

@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                Name
            </th>
             <th>
                Second Name
            </th>
            <th>
                attendance
            </th>
            <th>
                Comment
            </th>

        </tr>

        @for (var i = 0; i < Model.Count(); i++) {
            <tr>
                <td>
                    @Html.DisplayFor(m => Model[i].StudentID)
                </td>
                <td>

                    @Html.DisplayFor(m => Model[i].Attendance)
                    @Html.EditorFor(m => Model[i].Attendance)
                </td>
                <td>

                    @Html.DisplayFor(m => Model[i].CommentNote)
                    @Html.EditorFor(m => Model[i].CommentNote)
                </td>
            </tr>
            }

     </table>
     <input type="submit" value="save" />
}

“并非所有代码路径都返回值错误”正在使用 AnotherListEdit 函数突出显示,第二个函数在 HttpPost 之后。如果我在没有整个功能的情况下运行项目,则显示工作,并且正确的信息被传递到显示。

任何帮助将不胜感激!

4

2 回答 2

1

如果模型状态无效,AnotherListEdit 方法会发生什么?那就是缺少的……如果模型状态无效,则该操作不会返回“ActionResult”

[HttpPost]
    public ActionResult AnotherListEdit(IList<ClassInstanceDetail> list)
    {
        if (ModelState.IsValid)
        {
            foreach (ClassInstanceDetail editedClassInstanceDetail in list)
            {
                var tempBook = (from teacher in db.ClassInstanceDetails
                                where (teacher.ClassInstanceID == editedClassInstanceDetail.ClassInstanceID)
                                && (teacher.StudentID == editedClassInstanceDetail.StudentID)
                                select teacher).First();

                db.ApplyCurrentValues(tempBook.EntityKey.EntitySetName, editedClassInstanceDetail);
            }
            db.SaveChanges();
            return View(db.Teachers.ToList());
        }
       //HERE!!What view should return? any error messages?
        return View("View with no valid modelstate?");

        //Maybe?
        //return RedirectToAction("AnotherListEdit");

    }
于 2013-02-18T19:24:19.270 回答
0
if (ModelState.IsValid)
    { //you return something here } 

但如果模型状态无效,则不返回任何内容。错误必须来自那里

于 2013-02-18T19:23:59.950 回答