when overriding OnActionExecuting, how do I return a Json result without passing to action?
In your action method, return Json(object) to return JSON to your page. SomeMethod would be a javascript method that then evaluates the Json object returned. ContentResult by default returns a text/plain as its contentType.
You just have toinclude html(view) as one of the property in your json data. @Zach Yes, It's possible. You can return your html with model. Create a partial view and return the partial view with model instead of json result.
I find it useful to use Json.NET to generate the json output. This has many advantages, for example JSON properties can be hidden under certain conditions.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (/* whatever */)
{
var result = new ResultModel(); // your json model
ContentResult content = new ContentResult();
content.ContentType = "application/json";
content.Content = JsonConvert.SerializeObject(result);
filterContext.Result = content;
base.OnActionExecuting(filterContext);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With