5

I'm trying to submit form data with jQuery. I'm using ASP.NET WebMatrix. In a .cshtml file I have

@{
    // other code
    if(IsPost)
    {
        var item = new Item();
        item.Title = Request.Form["title"];
        item.Description = Request.Form["description"];

        // aditional code here
    }
}
<script type="text/javascript">
    $(document).ready(function(){
        $("form#itemForm").submit(function(){
            $.post("form.cshtml", {
                    title: $("#title").val(), 
                    description: $("#description").val(), 
                    price: $("#price").val()},
                    function(data){
                    },
                    "json");
        })
    });
</script>
<form>
<!-- html form here -->
</form>

How can I pass values from form to Request.Form object? And how can I than response with json back to html?

zigomir
  • 985
  • 2
  • 15
  • 30

3 Answers3

6

A better way would be to just have jQuery post the form data using $(this).serialize() instead of building an object with all the values in it it to pass. After that, yah, Request["title"], etc will get the values that were posted.

Erik Porter
  • 2,286
  • 1
  • 14
  • 8
2

This is what you want.

http://www.mikesdotnetting.com/Article/155/WebMatrix-And-jQuery-Forms

infocyde
  • 4,140
  • 2
  • 25
  • 36
0

Values are passed through jQuery.post() to Request.Parameters.

zigomir
  • 985
  • 2
  • 15
  • 30