Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post a string to mvc

I've seen how I can serialize to an object in JSON. How can I POST a string which returns a ViewResult?

            $.ajax({
                url: url,
                dataType: 'html',
                data: $(this).val(), //$(this) is an html textarea
                type: 'POST',
                success: function (data) {
                    $("#report").html(data);
                },
                error: function (data) {
                    $("#report").html('An Error occured.  Invalid characters include \'<\'. Error: ' + data);
                }
            });

MVC

   [HttpPost]
    public ActionResult SomeReport(string input)
    {
        var model = new ReportBL();
        var report = model.Process(input);
        return View(report);
    }
like image 544
P.Brian.Mackey Avatar asked Aug 26 '12 17:08

P.Brian.Mackey


People also ask

How to send post request in Web API?

The HTTP POST request is used to create a new record in the data source in the RESTful architecture. So let's create an action method in our StudentController to insert new student record in the database using Entity Framework. The action method that will handle HTTP POST request must start with a word Post.


1 Answers

How about:

        $.ajax({
            url: url,
            dataType: 'html',
            data: {input: $(this).val()}, //$(this) is an html textarea
            type: 'POST',
            success: function (data) {
                $("#report").html(data);
            },
            error: function (data) {
                $("#report").html('An Error occured.  Invalid characters include \'<\'. Error: ' + data);
            }
        });

If you make the data a JSON object with a key that matches the parameter name MVC should pick it up.

On the MVC side...

[HttpPost] 
public ActionResult SomeReport() 
{ 
    string input = Request["input"];
    var model = new ReportBL(); 
    var report = model.Process(input); 
    return View(report); 
} 
like image 152
Davy8 Avatar answered Oct 12 '22 10:10

Davy8