Reputation: 65
I am doing a simple AJAX POST from CSHTML. In the post, I am passing a string.
$.ajax({
type: "POST",
url: '@Url.Action("Test","Controller"',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ pt: 'testString' }),
cache: false,
success: function(data) {
}
});
I want to get this value in controller as follows -
[HttpPost]
public JsonResult Test()
{
var pt = string.empty;
TryUpdateModel(pt);
// Do some processing and return a value
return Json(true);
}
But always my pt
comes as empty. Also please do give me solution with works with value types as well.
Upvotes: 0
Views: 307
Reputation: 901
try this:
[HttpPost]
public JsonResult Test()
{
var pt = Request.Params["pt"]
// Do some processing and return a value
return Json(true);
}
OR
class PtModel { public string pt { get; set; } }
[HttpPost]
public JsonResult Test()
{
var ptModel = new PtModel();
TryUpdateModel(ptModel);
var language = ptModel.pt;
// Do some processing and return a value
return Json(true);
}
TryUpdateModel correct works only for objects with properties
Upvotes: 1
Reputation: 4147
The method for the controller should be
[HttpPost]
public ActionResult Test(string pt)
{
TryUpdateModel(pt);
// Do some processing and return a value
return Json(true);
}
Upvotes: 0