1

I'm really new to programming and stuck on a problem.

I'm trying to edit and update multiple rows of a database in one view, using mvc and asp.net. I think I'm somewhere along the right tracks but keep getting an error saying "not all code paths return a value". My Conroller looks like this:

 [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());
        }


    }

My View looks like this:

    @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" />
}

The "not all code paths return a value error" is being highlighted with AnotherListEdit function, the second one thats after HttpPost. If I run the project without that whole function, the display works, and the correct information is passed to the display.

Any help would be very much appreciated!!

2 Answers 2

1

What should happen in the AnotherListEdit method if the modelstate is invalid? That is what is missing ... The action does not return a "ActionResult" if the modelstate is invalid

[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");

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, can't believe how simple the solution was!
0
if (ModelState.IsValid)
    { //you return something here } 

but if the modelstate is not valid, nothing is returned. The error must come from there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.