I'm building a webapp using ASP C#.NET MVC3.
I have a View (.cshtml file). In this View I have a javascript function "SomeFunction()" which calls a .NET function through Razor like so: "@Html.Raw(Json.Encode(Model))". So everything put together it looks like:
SomeFunction(){
var sections = @Html.Raw(Json.Encode(Model));
}
Note the @ please.
This function throws an exception (this exception to be exact: Increase json response maxJsonLength in MVC 4) when the size of the string exceeds the maximum (too many sections basically). Well the requirements of my website don't instruct me to actually deal with this (luckily) by retrieving the data in pieces async and whatnot. I'm fortunate enough to simply catch this exception and tell the user: "Too many sections! Please define a larger size for sections." Or something like this. Alas I don't really understand how to do this exactly. I've tried the following:
SomeFunction(){
try {
var sections = @{try {
Html.Raw(Json.Encode(Model));
} catch (Exception e) {
throw new Exception("Exception caught in .NET");
}}
} catch(err) {
alert('.NET exception caught in Javascript!');
}
}
Note the @ infront of the 'try' to denote Razor syntax.
The 'alert' in the catch clause in javascript is never reached. I come from desktop dev so I'm still a web novice. How is this normally done?
Thanks a lot in advance for helping me.