JSON结果提示"另存为"正在处理的浏览器,而不是对话。 ASP.NET MVC [英] Json results prompting "Save As" dialog in browser instead of being processed. ASP.NET MVC

查看:109
本文介绍了JSON结果提示"另存为"正在处理的浏览器,而不是对话。 ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个问题的人,但我还没有找到任何解决我的问题。

I know this has been an issue for others, but I've yet to find anything that fixes my problem.

我有一个显示在收藏夹(颜色框)的局部视图。这是一个简单的形式。我想要的形式提交,并返回数据的一点点。这些数据将在以后的调用函数中使用,我想主要的DIV刚与一个成功的消息更新。下面是部分视图的全code:

I have a partial view that is displayed in a lightbox (colorbox). It is a simple form. I want the form to submit and return a little bit of data. The data will be used in calling subsequent functions, and I want the main DIV just to be updated with a "success" message. Here is the full code of the partial view:

<%@ 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>

    <% } %>

下面是我的控制器code。的code ++工程,因为它成功地将表单数据添加到表/数据库中。究竟要我的回归路线是什么样子?

Here is my controller code. The code works, in that it successfully adds the form data to the table/database. What exactly should my "return" line look like?

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

目前,我实际上没有使用任何的结果的数据在我的部分观点code(即使我引用它在我的成功参数)。我只是想获得它的工作(而不是提示我保存的结果)。

Currently I'm not actually using any of the result data in my partial view code (even though I reference it in my "success" parameter). I'm just trying to get it to work (and not prompt me to save the results).

感谢。

推荐答案

通常你不会为JSON直接通过正常的表单提交返回到用户的浏览器。这是一个伟大的方式来传递你打算在内部消化(正如你所描述)的信息,但这样做,你看到前面会有什么JSON数据吐回它只是看起来像.... Javscript用户 - 这不是有效的HTML,并根据浏览器是如何配置的,它可能会弹出另存为对话框。

Typically you're not going to return Json to the user's browser directly through a normal form submittal. It's a great way to pass information that you plan to digest internally (as you describe) but doing what you show above will spit back Json data to the user which just looks like.... Javscript -- it's not valid HTML and depending on how the browser is configured, it may pop-up a "Save As" dialogue.

在你的榜样,你可以尝试有默认的操作是返回创建视图,并有一个这样的检查:

In your example, you could try to have the default action be to return the Create view and have a check like this:

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

...并改变你的函数的返回类型为的ActionResult

这将确保页面初始加载显示实际查看。 Ajax调用同一个页面(从页面上的按钮)将返回刚才您感兴趣的JSON数据。

This will ensure that the initial load of the page displays the actual View. Ajax calls to the same page (from buttons on the page) will return just the Json data that you're interested in.

更新:

如果操作应显示除了更新的Json内容相同的页面,我建议不使用一个提交按钮。只需使用一个正常的输入按钮并绑定点击动作到你的 jQuery.ajax 通话。这些数据将被从收到创建/ POST 动作和每个成功将填充:在Ajax调用条款。

If the action should display the same page except for the updated Json content, I'd recommend not using a Submit button. Just use a normal input button and bind the click action to your jQuery.ajax call. The data will be received from the Create/POST action and will populate per the success: clause in the Ajax call.

这篇关于JSON结果提示&QUOT;另存为&QUOT;正在处理的浏览器,而不是对话。 ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆