Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to update several rows in my database through a form. Th values are in foreach loop. I am trying to update several rows for the field 'Answer' in the form. Please find the code below:
View:
@model IEnumerable<VOC.Models.QuestionnaireFeedback>

@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm("updateFeedback", "Questionnaire", FormMethod.Post))
{
      @foreach(var item in Model){
          @Html.DisplayFor(modelItem => item.ProjectID)
          @Html.DisplayFor(modelItem => item.FeedbackNo)
          @Html.EditorFor(modelItem => item.Answer)
      }
<input type="submit" value="Save" />
}

Controller:
<pre>[HttpPost]
        public ActionResult updateFeedback(List<QuestionnaireFeedback> fromQuestionnaire)
        {
            if (ModelState.IsValid) {
                FeedbackBusinessLayer feedbcakBusLayr = new FeedbackBusinessLayer();
                feedbcakBusLayr.updateFeedback(fromQuestionnaire);
                return RedirectToAction("Index");
            }
            
            return View(db.QuestionnaireFeedbacks.ToList());
        }


I am relatively new to the concept of MVC. But,I debugged the code, in the Controller fromQuestionnaire is null.
I have been stuck for a while now. Any help will be appreciated.
Posted

1 solution

Rather than foreach in your view you need to use an array-based approach.

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/[^]
 
Share this answer
 
Comments
TrishaC 3-Sep-15 11:00am    
@Html.EditorFor(modelItem => item.Answer) retains the value that i enter but the following values are coming as null:

@Html.DisplayFor(modelItem => item.ProjectID)
@Html.DisplayFor(modelItem => item.FeedbackNo)

I need these values in my controller as i display them on the view.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900