ASP.NET MVC : get values from FormData() in controller

Viewed 37

I'm using FormData() to send data from my Ajax to an ASP.NET MVC controller.

The problem is I have no idea how to get this data in my controller. I successfully get file data with

var pic = System.Web.HttpContext.Current.Request.Files["MyImages"];

but I get nothing when try to do same with text values.

 var data3 = new FormData();
 var dtime = Date.now();
 var newD = dtime.toString();
 data3.append("DateTime", newD);

 $.ajax({
        url: "/Adopt/Tst",
        type: "POST",
        contentType: false,
        processData: false,
        data: data3,
        success: function (response) {
            alert("done");
            console.log(response);
        }
    });
1 Answers

Use the FormCollection class:

[HttpPost]       
public ActionResult Tst(FormCollection fc)
{
    var provider = fc.ToValueProvider();
    if (provider.ContainsPrefix("DateTime"))
    {
        var dt = provider.GetValue("DateTime").AttemptedValue;
        // using the `dt`
    }
    return Json("Success"); 
}
Related