How can I update a view from within a standard method defined within a controller? The method is being called from a jquery plugin that I hookup.
View:
@model ViewModel
@Html.EditorFor(model => model.First)
@Html.EditorFor(model => model.Last)
@Html.EditorFor(model => model.Junk)
Controller Method:
// Called from third party jquery plugin
public string DoSomeWorkAndUpdateMyView(string Id)
{
// Get persisted View from dbcontext...
// Create a new Junk object with the new id and add it to the db context
// Update the view with the newly added junk object
ViewModel model = dbContext.GetViewStuff();
model.Junk.Add(new junk);
return View("JunkView", model);
}
Usage:
...
onComplete: function (event, queueID, fileObj, response, data)
{
$.ajax(
{ url: '@Url.Action("ProcessForm","Home")',
data: { first: $("#fname").val() },
success: function (data) {
$("#fname").val(data);
}
})
}
...
I don’t think there is an easy way to return your Model in an Ajax call and have the View updated the way you are thinking about it. So you can either to a regular POST/GET, or do a little work around:
Then on the receiving end check the response status. If ‘success’ then use
document.location=to just reload the whole page.If you must do it in one call, you will need to do something like
and then manually set the value of each element on the client side.