What you're asking for
The functionality you're asking for is AJAX. AJAX requests are 'Asynchronous', which at the most basic level means HTTP requests can be initiated and responded to without the need to refresh the page.
As someone wrote in comment to your question, jQuery can be used and does provide a nice way to do AJAX requests, but a lot of people would probably cry if you included the entire jQuery library just for an AJAX request.
JavaScript
So in JavaScript this is a liiiiiittle bit more complicated, there are some examples here. I'm not going to show you how to do it in JavaScript simply because I don't have a lot of time right now, but I might update the answer later. I would probably advise looking into doing it this way if you can.
jQuery
In jQuery this is easy. The documentation for AJAX requests is here.
Basically what you need to do is make a request to the server so it can update your partial view data and then return the new partial view for you to replace your current HTML with. It would look something like:
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$.ajax({
url: '@Url.Content("~/Controller/_CurrentData")',
type: 'POST',
data: {
//partialViewForm relates to the form element in your partial view
model: JSON.stringify($('#partialViewForm').serializeObject());
},
success: function (response) {
if (response) {
//partialViewDiv relates to the div in which your partial view is rendered
$('#partialViewDiv').html(response);
}
},
error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }
});
The above would assume you had something like this as your controller method:
[HttpPost]
public ActionResult _CurrentData(ItemDetails model)
{
//do stuff with model here
return PartialView("_CurrentData", model);
}
So this is basically how you'd contact the controller. In order to call that ajax from your webpage, you'd need to have a button in your partial view within the form that you can override using event.preventDefault()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…