I am trying to implement some save functionality via AJAX. I have a view with controls defined like below to be populated from the model:
@Html.LabelFor(model => model.mediaName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.mediaName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.mediaName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.mediaName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.mediaName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.mediaName, "", new { @class = "text-danger" })
When a user modifies the data and clicks the save button, the View is able to build up the new data from those controls and send it to the Controller (which simply saves the model to the db).
I am trying to do the same but via AJAX but I am having problems accessing an "already-built" model from the controls...
function saveDetails() {
var model = //HOW I GET THE MODEL?
$.ajax({
url: '/Media/SaveDetails',
dataType: 'html',
data: {
obj: model
},
success: function (data) {
}
});
}
Is there any way I can access that model? Or I should build it control-by-control?
EDIT: This is how I expect the controller to get the action: (Medium is the entity used in the Model)
public ActionResult SaveDetails(DomainModel.Medium obj)
{
db.Entry(obj).State = EntityState.Modified;
db.SaveChanges();
return PartialView("_MediaModalDetails", obj);
}