0
 @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 ?

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

also for displying a table he could use the WebGrid from the WebHelper Class tha recieves only an IEnumerable of your data and knows what to do. msdn.microsoft.com/en-us/library/…
If he created an editor template, he will be able to pass the model to a controller?
@aquinas - Yes. I have used this myself to get models that contain collections passed in as a single strongly typed parameter to an action.
I guess I thought he wanted to be able to edit one of the ManagementActions in the list. Maybe I'm wrong...
0

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.
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.