User1674987 User1674987 - 1 month ago 8
jQuery Question

Always getting NULL in controller when passing values from JavaScript

My requirement is to pass a

string
value from javascript to controller.
The ActionResult method is called properly but the parameter type of string always getting null.

JavaScript:

var contents = 'M1234';
$.get('/Sched/GetInformation/' + contents, {},
function (data, status) {
if (!data.success) {
alert('test');
}
});


Controller:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "")]
public ActionResult GetInformation(string mID)
{
bool superficialCheck = true;

return Json(new { success = superficialCheck },
JsonRequestBehavior.AllowGet);
}


Note:
Controller Name is SchedController

mID
is always getting
null
value

Answer

I think the problem is that the route doesn't have parameter mID defined.

If you're using the default route, that usually defines a parameter id. You can either name the paramater id in your action method, or specifically pass mID when you call from javascript or define a route with parameter mID.

Comments