简体   繁体   中英

How do I pass Model back up to Controller in ASP.NET MVC?

The model that is being displayed in my view has been filtered by user action so some of the original data is no longer there. Paying attention to the ActionLink for EditRow , this is how my view displays the model:

@foreach (var item in Model)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.User.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.User.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.User.Email)
    </td>
    <td>
        @item.Weekly.WeekBeginning.ToShortDateString()  -      @item.Weekly.WeekEnding.ToShortDateString()
    </td>
    <td>
        @(item.WeeklyTargetId != ViewBag.Edited ? Html.DisplayFor(modelItem => item.Hours) : Html.EditorFor(modelItem => item.Hours))
    </td>
    <td>
        //here is the line I am trying to pass the model through:
        @Html.ActionLink("Edit", "EditRow", new {id = item.WeeklyTargetId, currentSelection = Model}) |
        @Html.ActionLink("Details", "Details", new {id = item.WeeklyTargetId}) |
        @Html.ActionLink("Delete", "Delete", new {id = item.WeeklyTargetId})
    </td>
</tr>
}

In the ActionLink for EditRow I was able to easily pass the id of each item through, but when I added currentSelection = Model or even = Model.ToList() , the controller method receives a count of 0.

Here is the Action signature I am using:

//I've tried IQueryable<WeeklyTarget> as well
public ActionResult EditRow(int? id, List<WeeklyTarget> selection )

My question is how can I pass the current(filterd) model being displayed in the view to the controller action? Should I be going about this a different way?

This is not the correct way to implement this, but if you MUST do it this way you would be better off passing the Ids from each item in Model rather than the Model objects themselves. Then in the Edit (GET) method, you can get those full objects from the DB using those IDs.

The intention of this code is not clear to me though. If every row has an "Edit" link, then I would assume that clicking that would only care about that specific row and not the entire list. More detail would be helpful to better answer the question.

UPDATE: As discussed below, the best way to go about this would be with AJAX(recommend using jQuery). Upon click on a edit link, you should initiate a click handler in JS:

$(".editLink").click(function(){
   //Code Here
});

The logic for the click function will be to grab the data from the row and make an AJAX Post back to a controller action.

$.post(urlToAction, data, succuessFunction);

This will work perfectly for you, however you will have to do a lot of the data insert, update, delete operations manually. There are some pretty handy JS frameworks out there like Knockout JS which will handle this automatically for you after a little wiring/plumbing work up front.

I suggest you check those out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM