【问题标题】:My ASP.NET MVC AJAX request is opening my Content() in a new window我的 ASP.NET MVC AJAX 请求在新窗口中打开我的 Content()
【发布时间】: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}.jsjquery.unobtrusive-ajax.js文件(否则Ajax.BeginForm()作为正常形式)
  • @EvanMulawski 解决了!谢谢

标签: c# ajax asp.net-mvc


【解决方案1】:

这就是告诉它在新窗口中打开内容的原因:

 Content(newOrgName + " successfully added to database!", "text/html");

为什么不直接将字符串返回给 Ajax 调用,然后在 ajax 成功处理程序中显示消息应该出现的样子。

【讨论】:

  • 你能澄清一下“将字符串返回给 Ajax 调用”是什么意思吗? (对不起,我是新手)
  • return newOrgName + " 成功添加到数据库!";
  • 然后在 ajax 端你可以这样做:stackoverflow.com/questions/2403441/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2014-03-24
相关资源
最近更新 更多