如何使用此代码无法在我的asp.net mvc应用程序中命中控制器

时间:2011-04-05 20:52:41

标签: javascript jquery asp.net asp.net-mvc-2

<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我正在获得popupmessage说你已经选择打开这个对话?有什么我做错了吗?

感谢

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

尝试这一点 - 并确保首先加载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" />

答案 3 :(得分:1)

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

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