如何使用此代码无法命中我的 ASP.NET MVC 应用程序中的控制器

发布于 2024-10-30 18:19:01 字数 1480 浏览 2 评论 0原文

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    GOTO = function () {
        alert("yes");
        $.ajax({
            cache: false,
            type: "POST",
            url: "/Home/Index/",
            data: datastring,
            dataType: "json",
            success: function (data) {
                alert("Ohh Yaa Success");
            }
        });
    }
</script>

   <input type="button" value="submit" onclick="JavaScript:GOTO()" />

</asp:Content>

我的控制器 ActionResult 是这样的 JsonResult

  [HttpPost]
        public System.Web.Mvc.JsonResult Index(FormCollection collection)
        {
            //return Content("<xml>this is just test</xml>", "text/xml");
            //return Content("this is just test", "text/plain");

            if (Request.AcceptTypes.Contains("application/json"))
            {
                return Json(new { id = 1, value = "new" });
            }
            else if (Request.AcceptTypes.Contains("application/xml") ||
                     Request.AcceptTypes.Contains("text/xml"))
            {

            }
            if (Request.AcceptTypes.Contains("text/html"))
            {
                //return View();
            }

           return Json(new { foo = "bar", baz = "Blech" });
        }

我始终无法在此处返回 JsonResult 我收到弹出消息,说您已选择打开此对话框?我做错了什么吗?

谢谢

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    GOTO = function () {
        alert("yes");
        $.ajax({
            cache: false,
            type: "POST",
            url: "/Home/Index/",
            data: datastring,
            dataType: "json",
            success: function (data) {
                alert("Ohh Yaa Success");
            }
        });
    }
</script>

   <input type="button" value="submit" onclick="JavaScript:GOTO()" />

</asp:Content>

My Controller ActionResult is something like this
JsonResult

  [HttpPost]
        public System.Web.Mvc.JsonResult Index(FormCollection collection)
        {
            //return Content("<xml>this is just test</xml>", "text/xml");
            //return Content("this is just test", "text/plain");

            if (Request.AcceptTypes.Contains("application/json"))
            {
                return Json(new { id = 1, value = "new" });
            }
            else if (Request.AcceptTypes.Contains("application/xml") ||
                     Request.AcceptTypes.Contains("text/xml"))
            {

            }
            if (Request.AcceptTypes.Contains("text/html"))
            {
                //return View();
            }

           return Json(new { foo = "bar", baz = "Blech" });
        }

I am not able to return the JsonResult here allways I am getting popupmessage saying u have choosen to open this dialogue? is there something I am doing wrong?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

維他命╮ 2024-11-06 18:19:01

试试这个——并确保首先加载 jQuery。请注意通过 jQuery 应用处理程序(而不是内联)、序列化数据、在代码中动态生成 URL(而不是硬编码)以及从单击处理程序返回 false 以阻止正常表单提交的更改。

<script type="text/javascript">
    $(function() {
        $('input[type=button]').click( function() {
          var data = $('form').serialize();  // or however you get your data
          $.ajax({
              cache: false,
              type: "POST",
              url: "<%= Html.Action( "index", "home" ) %>",
              data: data,
              dataType: "json",
              success: function (data) {
                  alert("Ohh Yaa Success");
              }
          });
          return false; // don't do the normal submit
        });
    });
</script>

<input type="button" value="submit" />

Try this instead -- and make sure jQuery is loaded first. Note the changes to apply the handler via jQuery instead of inline, serializing the data, generating the URL in code dynamically rather than hard-coded, and returning false from the click handler to prevent normal form submission.

<script type="text/javascript">
    $(function() {
        $('input[type=button]').click( function() {
          var data = $('form').serialize();  // or however you get your data
          $.ajax({
              cache: false,
              type: "POST",
              url: "<%= Html.Action( "index", "home" ) %>",
              data: data,
              dataType: "json",
              success: function (data) {
                  alert("Ohh Yaa Success");
              }
          });
          return false; // don't do the normal submit
        });
    });
</script>

<input type="button" value="submit" />
智商已欠费 2024-11-06 18:19:01

您需要将按钮放在表单标签中并在 onsubmit 事件中调用 GOTO 函数

you need to put the button in a form tag and call the GOTO function in onsubmit event

不醒的梦 2024-11-06 18:19:01

看来您的 data: datastring 可能是问题所在。检查并确保数据参数的名称与方法参数的名称相同。

It looks like your data: datastring might be the problem. Check to make sure that the name of your data parameter is the same as your method parameter.

白衬杉格子梦 2024-11-06 18:19:01

我会尝试更像这样......

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    $(document).ready(function () {
        $(form).submit(function() {
        alert("yes");
        $.post({
            cache: false,
            type: "POST",
            url: "/Home/Index/",
            data: datastring,
            dataType: "json",
            success: function (data) {
                alert("Ohh Yaa Success");
            }
        });
      });
    }
</script>

<form>
   // your form fields
   <input type="button" value="submit" />
</form>
</asp:Content>

然后你的控制器应该看起来更像这样。

请注意我们如何将参数更改为与您的 jQuery data 字段匹配的字符串。

[HttpPost]
public System.Web.Mvc.JsonResult Index(string datastring)
{
    // you can deserialize your Json here.

    //return Content("<xml>this is just test</xml>", "text/xml");
    //return Content("this is just test", "text/plain");

    if (Request.AcceptTypes.Contains("application/json"))
    {
        return Json(new { id = 1, value = "new" });
    }
    else if (Request.AcceptTypes.Contains("application/xml") ||
             Request.AcceptTypes.Contains("text/xml"))
    {

    }
    if (Request.AcceptTypes.Contains("text/html"))
    {
        //return View();
    }

   return Json(new { foo = "bar", baz = "Blech" });
}

I would try to approach it more like this ...

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    $(document).ready(function () {
        $(form).submit(function() {
        alert("yes");
        $.post({
            cache: false,
            type: "POST",
            url: "/Home/Index/",
            data: datastring,
            dataType: "json",
            success: function (data) {
                alert("Ohh Yaa Success");
            }
        });
      });
    }
</script>

<form>
   // your form fields
   <input type="button" value="submit" />
</form>
</asp:Content>

And then your controller should look more like this.

Notice how we changed the parameter to a string that matches your jQuery data field.

[HttpPost]
public System.Web.Mvc.JsonResult Index(string datastring)
{
    // you can deserialize your Json here.

    //return Content("<xml>this is just test</xml>", "text/xml");
    //return Content("this is just test", "text/plain");

    if (Request.AcceptTypes.Contains("application/json"))
    {
        return Json(new { id = 1, value = "new" });
    }
    else if (Request.AcceptTypes.Contains("application/xml") ||
             Request.AcceptTypes.Contains("text/xml"))
    {

    }
    if (Request.AcceptTypes.Contains("text/html"))
    {
        //return View();
    }

   return Json(new { foo = "bar", baz = "Blech" });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文