當我在MVC3應用程序中進行特定調用時,我必須更新多個DOM元素(在本例中爲div)。我所有的行動結果是在不同的功能都用自己的視圖模型:通過只對ActionResult進行一次調用來更新多個DOM元素
public ActionResult Action3(ViewModel3 model)
{
return PartialView("_DomElement3", model);
}
public ActionResult Action2(ViewModel2 model)
{
return PartialView("_DomElement2", model);
}
public ActionResult Action1(ViewModel1 model)
{
return PartialView("_DomElement1", model);
}
下面是三個剃刀局部視圖樣子(只是用不同的模型):
@model ViewModel1
<div id='domElement1'>@Model.values1</div>
我做到了這因爲他們被要求召喚,我想沒有別的東西會觸發他們。但是我遇到了一個場景,我必須在滿足特定條件後更新所有三個div元素。目前,我正在做這種方式,我不喜歡這樣一個位:
var values_1 = {'values1':'one'};
$.ajax({
url: '@Url.Action("Action1")',
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: values_1,
success: function (data) {
$('#domElement1').html(data);
var values_2 = {'values2':'two'};
$.ajax({
url: '@Url.Action("Action2")',
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: values_2,
success: function (data) {
$('#domElement2').html(data);
var values_3 = {'values3':'three'};
$.ajax({
url: '@Url.Action("Action3")',
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: values_3,
success: function (data) {
$('#domElement3').html(data);
}
});
}
});
}
});
正如你所看到的,我在同一時間撥打電話到服務器之一。有沒有人知道更好的方式來更新我的元素只有一個電話到服務器?
用所有三種模型創建一個ViewModel並根據需要返回。 – Jose
@Jose - 我有點思考這些問題。如果可能,我仍在尋找另一種解決方案。感謝您回覆 – BoundForGlory