ASP.NET Core - The name 'JsonRequestBehavior' does not exist in the current context
Returning Json-formatted data:
public class ClientController : Controller
{
public JsonResult CountryLookup()
{
var countries = new List<SearchTypeAheadEntity>
{
new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada}
};
return Json(countries);
}
}
In Code it's replace To JsonRequestBehavior.AllowGet
with new Newtonsoft.Json.JsonSerializerSettings()
It's Work same as JsonRequestBehavior.AllowGet
public class ClientController : Controller
{
public ActionResult CountryLookup()
{
var countries = new List<SearchTypeAheadEntity>
{
new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"}
};
return Json(countries, new Newtonsoft.Json.JsonSerializerSettings());
}
}
some times you need to return a message back in json ,simply use the json result as below , no need for jsonrequestbehavior any more ,below simple code to use
public ActionResult DeleteSelected([FromBody]List<string> ids)
{
try
{
if (ids != null && ids.Count > 0)
{
foreach (var id in ids)
{
bool done = new tblCodesVM().Delete(Convert.ToInt32(id));
}
return Json(new { success = true, responseText = "Deleted Scussefully" });
}
return Json(new { success = false, responseText = "Nothing Selected" });
}
catch (Exception dex)
{
return Json(new { success = false, responseText = dex.Message });
}
}