简体   繁体   中英

My ASP.NET MVC AJAX request is opening my Content() in a new window

I was wondering whether you guys could help me figure out what I'm missing that is making my AJAX request return my Content in a new window. I was trying to follow the post Using Ajax.BeginForm with ASP.NET MVC 3 Razor like a recipe book and apparently I'm missing something.

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");
    }

M:

    </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>

Submitting, say, "StackOverflow" opens up a page with the source code nothing more than

Stack Overflow successfully added to database!

whereas my intention was my for that piece of text to be inside

<i id="new-org-output-msg"></i>

This is what is telling it to open the content in a new window:

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

Why not just return the string to the Ajax call and in the ajax success handler, display the message as it should appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM