如何不能够击中控制器在我的asp.net mvc应用程序使用此代码

How to not able to hit the controller in my asp.net mvc application using this code

本文关键字:应用程序 mvc net 代码 asp 我的 不能够 控制器      更新时间:2023-09-26
<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" });
        }

我不能返回json结果在这里总是我得到弹出消息说你已经选择打开这个对话框?我做错了什么吗?

谢谢

试试这个——并确保先加载jQuery。请注意这些变化:通过jQuery而不是内联应用处理程序,序列化数据,动态生成代码中的URL而不是硬编码,并从click处理程序返回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" />

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

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

我会尝试这样做…

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