0

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>
Community
  • 1
  • 1
  • 2
    Are you including jQuery and the Unobtrusive Ajax scripts? Those are required for this to work properly. – Evan Mulawski May 14 '15 at 19:08
  • You need to include the `jquery-{version}.js` and `jquery.unobtrusive-ajax.js` files (otherwise `Ajax.BeginForm()` acts as a normal form) –  May 14 '15 at 19:31

1 Answers1

0

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.

Karen B
  • 331
  • 2
  • 9