1
まず、私の悪い英語のために申し訳ありません。ajax.BeginFormでコンテンツを変更するにはどうすればよいですか?
ボタン "ページ1"をクリックすると、コントローラーは "ページ1"のレンダリング部分と "ページ2"と "すべてのページ"の同じものを返します。
@{
ViewBag.Title = "Title";
}
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<h2>Title</h2>
@using (Ajax.BeginForm("NameAction", //controller action name
"NameController", //controller name
new AjaxOptions //ajax options that tell mvc how to perform the replacement
{
UpdateTargetId = "ViewPage", //id of div to update
HttpMethod = "Post" //how to call the controller action
}, new { id = "FormName" }))
{
<input type="submit" id="btn" value="p1" id="p1"/>
<input type="submit" id="btn" value="p2" id="p2"/>
<input type="submit" id="btn" value="AllPage" id="AllPage"/>
<div id="ViewPage">
//render partial view
</div>
}
そして、私のコントローラは、次のとおりです:
私の見解ではあり
public class NameController : Controller
{
[HttpPost]
public ActionResult NameAction(String btn)
{
if (Request.IsAjaxRequest())
if(btn="p1")
return PartialView("p1");
if(btn="2")
return PartialView("p2");
if(btn="3")
return PartialView("p3");
return View();
}
}
Request.isAjaxRequestは常にfalse等しいとpartialviewはdiv要素を更新しますが、すべてのページに用
感謝を消去しませんあなたのお手伝い。
MVC2またはMVC3:
Ajax.BeginFormは、AJAX要求を動作し、送信するように、あなたのページに
jquery.unobtrusive-ajax
スクリプトが含まれていることを確認してください?質問のタグを整理してください。 – ThiefMaster@ThiefMaster、私は 'razor'で使用できない' asp.net-mvc-2'タグを削除しました。 –
タグにご迷惑をおかけして、訂正していただきましたダーリン・ディミトロフに感謝いたします。それは私の "文字列btn"の解決策がnullではないが、ajaxオプションが機能しないためです:(コントローラのisAjaxRequestが常にfalseであるとテストすると、partialviewはすべてのページを消去し、div:sを更新しません – Zoners