4

I wanna update a list of item in edit page, a user pass to edit page and update request for a list of question, a model for request is ready to use, edit.cshtml is like

@using (Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
    @foreach (var item in Model.Requesttables)
    {
        <div class="editor-label">
            @Html.LabelFor(modelItem => item.request)
        </div>
        <div class="editor-field">
            @Html.EditorFor(modelItem => item.request)
            @Html.ValidationMessageFor(modelItem => item.request)
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    }
</fieldset>
}

how the controler will be??

public ActionResult Edit(List <Requesttable> requestlist)
    {//some logic here!}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
  • Your use of a `foreach` loop means you wont be able to bind to anything on post back. you need a `for` loop or custom `EditorTemplate` to generate controls in a collection. Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). And the parameter in the POST method needs to be the same model as you passed to the view –  May 07 '15 at 11:19

1 Answers1

1

If I understand it right, you want to see the controller. First I think that there is something wrong with your controller signature. It should be like this:

public ActionResult Edit(int id)
{
//search the object, no matter what it is - as long as it is form database by id
var db = new DbContext();
var yourRequestedList=db.Find(id); //or something like that, see linq for the correct sintax

yourRequestedList = objectThatWasEdited; 
}

I hope this helps you out, and don't forget to refactor. My cod is not a good practice, you do not instatiate the db inside of a controller method.

Liviu Sosu
  • 1,381
  • 3
  • 15
  • 26
  • No, This doesn't answer question as OP here seems to post list of Model back to the server and save it in some persistent storage. – Jenish Rabadiya May 07 '15 at 11:13