开发者

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

<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.AcceptTy开发者_JAVA技巧pes.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


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" />


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜