MVC 3 - Ajax.BeginForm 做一个完整的回发 [英] MVC 3 - Ajax.BeginForm does a full post back
问题描述
在以下代码中,我使用 Ajax.BeginForm 将数据异步发布到操作.该操作被调用,但结果显示在一个新的网页上.我看过很多例子.这似乎并不难.对于概念证明 (poc),我已经使示例变得非常简单,但我仍然看到显示了一个新页面.
控制器
[HttpPost][OutputCache(Location = OutputCacheLocation.None, NoStore = true)]公共字符串 TestAjax(UserViewModel viewModel){返回 viewModel.UserName;}
查看
@model BasicMvc3Example2.Models.UserViewModel@{ViewBag.Title = "Index2";Layout = null;//"~/Views/Shared/_Layout.cshtml";}<script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script><script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script><script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script><script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script><h2>索引2</h2><script type="text/javascript">函数 PostFailure(){警报(失败");}函数 PostSuccess(){警报(成功");}函数 PostOnComplete() {警报(完成");}页面渲染:@DateTime.Now.ToLongTimeString()@using (Ajax.BeginForm("TestAjax", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "textEntered", OnFailure = "PostFailure", OnSuccess = "PostSuccess", OnComplete = "PostOnComplete" })){<div>@Html.LabelFor(m => m.UserName)@Html.TextBoxFor(m => m.UserName)
<div>@Html.LabelFor(m => m.Password)@Html.TextBoxFor(m => m.Password)
<div>@Html.LabelFor(m => m.EmailAddress)@Html.TextBoxFor(m => m.EmailAddress)
<input type="submit" id="submitForm" value="提交表单"/>}<div id="textEntered">d</div>
你能检查 _Layout.cshtml
并确保引用了 ajax 脚本吗?我不认为它是默认的:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
In the following code, I'm using Ajax.BeginForm to post data to the action asynchronously. The action gets called but the results are displayed to a new web page. I'v looked at a ton of example. This doesn't seem difficult. I've made the example extremely simple for a proof of concept (poc), but I'm still seeing a new page displayed.
Controller
[HttpPost]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public string TestAjax(UserViewModel viewModel)
{
return viewModel.UserName;
}
View
@model BasicMvc3Example2.Models.UserViewModel
@{
ViewBag.Title = "Index2";
Layout = null;//"~/Views/Shared/_Layout.cshtml";
}
<script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
<h2>Index2</h2>
<script type="text/javascript">
function PostFailure(){
alert("Failure");
}
function PostSuccess(){
alert("Success");
}
function PostOnComplete() {
alert("Complete");
}
</script>
Page Rendered: @DateTime.Now.ToLongTimeString()
@using (Ajax.BeginForm("TestAjax", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "textEntered", OnFailure = "PostFailure", OnSuccess = "PostSuccess", OnComplete = "PostOnComplete" }))
{
<div>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</div>
<div>
@Html.LabelFor(m => m.Password)
@Html.TextBoxFor(m => m.Password)
</div>
<div>
@Html.LabelFor(m => m.EmailAddress)
@Html.TextBoxFor(m => m.EmailAddress)
</div>
<input type="submit" id="submitForm" value="Submit Form" />
}
<div id="textEntered">d</div>
Can you check _Layout.cshtml
and make sure the ajax script is referenced? I don't think it is by default:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
这篇关于MVC 3 - Ajax.BeginForm 做一个完整的回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!