0

So, I am new to MVC, and I have a partial view with IEnumerable model, in which I have few fields I would like to be editable.

@model IEnumerable<Allocations>
<p>
    @Html.ActionLink("Create New", "AddAllocation", "Admin")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Percentage)
        </th>        
    </tr>

@foreach (var item in Model) {  

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>      
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>    
        <td>
            @Html.EditorFor(modelItem => item.Percentage)           
        </td>           
       <td>@Html.HiddenFor(modelItem => item.ID)</td>
        <td>            
            @Html.ActionLink("Save", "EditAllocation", "Admin", null, new { id= item.ID }) 
        </td>
    </tr>       

}

</table>

I want to be able to pass the entered/changed textbox values to the controller action. I understand, I can do that using JQuery if it wasn't a IEnumerable model view. I might be missing something here, and would appreciate if someone could direct me in the right direction.

Ratan
  • 863
  • 5
  • 12
  • 28
  • 3
    You should never be passing a collection of complex objects to a GET method - apart from the ugly query string it would generate, you could easily exceed the query string limit and throw an exception. Use a form and post the data. But you need to use a custom `EditorTemplate` for your type because your current `foreach` loop is generating duplicate `id` attributes (invalid html) and duplicate `name` attributes which will never bind correcty –  Jun 30 '15 at 22:57
  • why not having a view model which has a field is a List, that's should be simple, you can just wrap your stuff in a form, then submit, – Kevin Simple Jun 30 '15 at 23:01
  • Also, the GET action (ActionLink) that saves or makes back-end changes is a bad idea -- web crawlers might visit that link (and perform a save). Make that a form as a POST submission. – Jasen Jun 30 '15 at 23:15
  • [Here's](http://stackoverflow.com/questions/17037858/how-to-pass-ienumerable-list-to-controller-in-mvc-including-checkbox-state) how to submit an `IEnumerable`. – Jasen Jun 30 '15 at 23:17

0 Answers0