Passing data from jquery to mvc controller

How passing data from jquery to mvc controller?

  
public class ResponseController : Controller
{
[HttpGet]
public ActionResult JavaScript()
    {
        return View();

    }
[HttpPost]
 public ActionResult JavaScript(string AJAXParameter1)
    {
       
            return View( AJAXParameter1);
    }

}

click [HttpGet] JavaScript() action method?


From above click on Add View option create view

@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnSubmit').click(function () {
                var name = $('#Ename').val();
                $.ajax({
                    type: 'POST',

                    url:"Response/JavaScript",

                    data:  "{'AJAXParameter1':'" + $('#Ename').val() +  "'}"

                    contentType: 'application/json; charset=utf-8',

                    dataType: "json",

                    success: function (data) {

                        alert(data);
                    }
                })
                alert("am");
            })
        });

    </script>
    <title>JavaScript</title>
</head>
<body>
   @using (Html.BeginForm("JavaScript","Response",FormMethod.Post))
    {
        <text>ename</text>
        <input type="text" id ="Ename" />
        <input type="button" id="btnSubmit" style="width:80px" value="Submit">
        <p>i am testing</p>
    }
</body>
</html>


View Display

Enter employee name click on submit button.

Now request hits [HttpPost] method.













Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

1 comment: