【发布时间】:2015-05-14 18:45:32
【问题描述】:
我想知道你们是否可以帮助我弄清楚我缺少什么让我的 AJAX 请求在新窗口中返回我的内容。我试图像一本食谱书一样关注Using Ajax.BeginForm with ASP.NET MVC 3 Razor 的帖子,显然我错过了一些东西。
C:
[HttpPost]
public ActionResult AddOrganization ( string newOrgName )
{
PortalData PD = new PortalData();
if ((from thisorg in PD.orgs where thisorg.orgname == newOrgName select thisorg).Count() > 0)
{
return Content("Organization name '" + newOrgName + " 'already exists in database!", "text/html");
} // error if that organization exists
PD.orgs.InsertOnSubmit(new Organization { orgname = newOrgName });
try
{
PD.SubmitChanges();
} // try submitting to db
catch (Exception e)
{
return Content(e.Message, "text/html");
}
// if made it here, everything is good
return Content(newOrgName + " successfully added to database!", "text/html");
}
男:
</script>
<h3>Or Add New Organization</h3>
@using (Ajax.BeginForm("AddOrganization", "FileUpload", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "new-org-output-msg" }))
{
<input type="text" name="newOrgName" />
<input type="submit" value="Add" />
}
<p><i id="new-org-output-msg"></i></p>
提交,比如说,"StackOverflow" 会打开一个页面,其中仅包含源代码
Stack Overflow successfully added to database!
而我的意图是让那段文字出现在里面
<i id="new-org-output-msg"></i>
【问题讨论】:
-
您是否包含 jQuery 和 Unobtrusive Ajax 脚本?这些是正常工作所必需的。
-
你需要包含
jquery-{version}.js和jquery.unobtrusive-ajax.js文件(否则Ajax.BeginForm()作为正常形式) -
@EvanMulawski 解决了!谢谢
标签: c# ajax asp.net-mvc