首先,抱歉我的英语不好。
我希望当一个人点击“Page1”按钮时,控制器会返回“Page1”的渲染部分以及“page2”和“allPage”的相同内容。
我的观点是:
@{
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但是擦除所有页面
感谢您的帮助。
答案 0 :(得分:1)
为您的提交按钮命名:
<input type="submit" name="btn" value="page1" id="Page1"/>
<input type="submit" name="btn" value="Page2" id="Page2"/>
<input type="submit" name="btn" value="AllPage" id="AllPage"/>
然后:
[HttpPost]
public ActionResult NameAction(string btn)
{
if (btn == "page1")
{
// the page1 button was clicked
}
else if (btn == "page2")
{
// the page2 button was clicked
}
else if (btn == "AllPage")
{
// the AllPage button was clicked
}
...
}
如果您不想依赖按钮的实际标签:
<button type="submit" name="btn" value="p1" id="Page1">Show page 1</button>
<button type="submit" name="btn" value="p2" id="Page2">Show page 2</button>
<button type="submit" name="btn" value="all" id="AllPage">Show all pages</button>
在控制器中你可以检查值。
更新:
确保已将jquery.unobtrusive-ajax
脚本包含在页面中,以便Ajax.BeginForm可以工作并发送AJAX请求:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>