我對在MVC中使用Ajax的正確方法有點困惑。假設我有一個返回相同視圖和相同模型的動作。唯一的問題是如果它是一個Ajax調用,我需要重新排列視圖的佈局。我發現解決方案要麼是我可以在MVC Action中確定它,並且返回相應的視圖,否則我可以轉到我的視圖並相應地使用@if(IsAjax)
和佈局項目。如果你正在做Ajax,你應該做兩個單獨的操作還是做單獨的視圖?
@model MyModels.SomeModel
@if (!IsAjax)
{
arrange this way.....
}
else
{
arrange another way...
}
[AllowAnonymous]
public ActionResult Details(int? id)
{
///get the SomeModel via some code here
if (!Request.IsAjaxRequest())
{
return View2(someModel)
}
return View1(someModel)
}
一種主觀問題,但我認爲這不是視圖的責任,要知道請求是否是AJAX請求。我會在控制器動作中執行這個邏輯。 –