Json results suggest a Save As dialog in the browser instead of processing. ASP.NET MVC

I know this is a problem for others, but I still need to find something that fixes my problem.

I have a partial view that displays in a lightbox (colorbox). This is a simple form. I want the form to represent and return some data. The data will be used when calling subsequent functions, and I want the main DIV to just be updated with the message “Success”. Here is the full partial view code:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Solution2.Models.Category>" %>

<script type="text/javascript">
    $('propcatform').submit(function(e) {
        e.preventDefault();

        $.ajax({
            type: "POST",
            url:  $(this).attr("action"),
            data: $(this).serialize(),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function(data) { document.getElementById('main1').innerHTML = "Success"; },
            failure: function() { document.getElementById('main1').innerHTML = "Failure"; }
        })
    });
    </script>

    <% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "propcatform", name = "propcatform" }))
       {%>
        <div id="main1">
        <fieldset>
            <legend>Fields</legend>
            <p>
                <label for="CatTitle">Category Title:</label>
                <%= Html.TextBox("CatTitle") %>
                <%= Html.ValidationMessage("CatTitle", "*") %>
            </p>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
        </div>

    <% } %>

Here is my controller code. The code works because it successfully adds form data to a table / database. What should be my return line?

[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Create(Category propcat)
        {

            Category resultcat = new Category();
            _db.Categories.InsertOnSubmit(propcat);
            _db.SubmitChanges();
            resultcat = propcat;
            return Json(new { CatID = resultcat.CatID, CatTitle = resultcat.CatTitle, message = "Category successfully created!" });
        }

- ( "success" ). ( ).

.

+3
3

, Json . , ( ), , , Json , ... Javscript - HTML , " ".

Create ​​:

if (Request.IsAjaxRequest())
{
  return Json(.......);
}
else
{
  return View(....);
}

... ActionResult.

, . Ajax ( ) Json.

UPDATE:

, Json, "". click jQuery.ajax. Create/POST success: Ajax.

+5

, :

return new JsonResult
{
  ContentType = "text/html",
  Data = new { foo = 1, bar = "Something" }
};

JsonResult, ContentType , Json.

+1

Firefox + Firebug.

-2

Source: https://habr.com/ru/post/1723026/


All Articles