Frederik Smet
Frederik Smet

Reputation: 1

passing model from view to controller in mvc3 c#

 @foreach (var item in Model.AllManagementActions)
{
 <tr>
     <td>
         @Html.DisplayFor(modelItem => item.Id)
     </td>
     <td>
         @Html.DisplayFor(modelItem => item.Name)
     </td>      

I want to pass all these values to my controller. The ID , the name, etc. Can't i just pass the item itself? This is my current code, but it doesn't work.

<a class="complete" title="My Action" data-url="@Url.Action("Submit", "Period", new { tpId = Model.Id ,it = item})" style="cursor: pointer;">       

It works if I pass item.name, item.id,item.number,etc

Can i pass the model ?

Upvotes: 0

Views: 2407

Answers (2)

Oded
Oded

Reputation: 498904

Don't iterate in your views - use editor templates for the type.

Have a '~/shared/EditorTemplates/ManagementAction.ascx' (or .cshtml if using razor) that renders a single ManagementAction.

In the view, instead of iterating use:

@Html.EditorFor(model => model.AllManagementActions)

Upvotes: 3

Freeman
Freeman

Reputation: 5801

yes you can pass the entire model, but you should use a submit button instead of of an anchor tag and also put your code inside a

using(Html.BeginForm)
{
   @foreach (var item in Model.AllManagementActions)
   {
      //your desire.
   }
   <input type="submit" value="Save" />
}

in your controller you will recieve the model on post

 [HttpPost]
 public ActionResult SaveViewData(YourModel model)
 {
    //your logic.
 }

Upvotes: 0

Related Questions