1

I am using .Net MVC5 and I am trying to create an Index view with an Edit Link(or button) that will POST(so I can't use ActionLink) an entire Model item Entity from the list of entities presented in the View. How do I do it?

my code(so far) is below

@model IEnumerable<Projname.Models.MyEntity>
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>

            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>Hello there</th>
            <th>life is good</th>
            </tr>

    @foreach (var item in Model) {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>

                @using (Html.BeginForm("Edit", "Edit", FormMethod.Post))
                {
                    @Html.Hidden(mytem => item);

            <input type="submit" value="Edit" class="btn btn-default" />
                }
            </td>
            <td>                
                @Html.ActionLink("Details", "Details", new { id = item.PrimeKey }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.PrimeKey })
            </td>
            </tr>
    }

    </table>
</body>
</html>

I am assuming that there is a better way to do it instead of creating a hidden field for each of the properties of the entity.

JOW
  • 244
  • 4
  • 18
  • 1
    Pass just the ID/primary key of myItem and re-retrieve the item from the database when you pass it to your Edit View. – AWinkle May 16 '16 at 14:39
  • Why pass the whole model, just the Id will do – scottdavidwalker May 16 '16 at 14:40
  • 1
    @AWinkle normally I'd do that, but since I am using Azure Tables, only specifying the Row Key(primary key) in the query would lead to an undesirable full table scan http://stackoverflow.com/questions/19715885/azure-tables-query-by-rowkey-as-condition. so I am trying to avoid it as much as i can. – JOW May 16 '16 at 14:51
  • 1
    @JOW From my knowledge, and the answers below, I think you are going to have to wrap everything in a hidden field. Since this is during the view rendering process, you can use a JSON serializer (e.g. NewtonSoft) to serialize the object into a singular hidden field. This will limit risks to model changes and make it easier to code, though it is likely less performant than using HiddenFor for each field. – AWinkle May 16 '16 at 18:12
  • 1
    Firstly you cannot use a `foreach` loop to generate form controls for a collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). Secondly, look at the html you generating in the `@Html.Hidden(mytem => item);` to understand why this can never work (you would need to generate an input for each property in the model). Just pass the `ID` property. –  May 17 '16 at 00:01
  • I ended up passing the RowKey and PartitionKey as Hidden fields. thanks for the help @StephenMuecke and AWinkle – JOW May 17 '16 at 09:59

2 Answers2

0

You need to put the items you want to edit or change into a form

<form class="form-horizontal" action="GetItemsFromView" method="post">
    <input type="text" name="EditedModelItem">
</form>

Afterwards you can call any edited item in your controller, through string hold = Request.Form["EditedModelItem"]; in the GetItemsFromView method.

DGK
  • 2,947
  • 5
  • 32
  • 47
0

Wrap everything in:

@using (Html.BeginForm("Edit", "Edit", FormMethod.Post))
{                
}

Then:

 public ActionResult Edit(Model model)
 {
      if (ModelState.IsValid)
      {
      }
 }

Or:

public ActionResult Edit(FormCollection formCollection)
{
}
David
  • 641
  • 1
  • 8
  • 23