Hari Subramaniam
Hari Subramaniam

Reputation: 1876

ASP.NET MVC 4 - Portal site

I am new to MVC4 and please bear with me. I am building a portal where users will be shown different widgets. Lets just say widgets are some rectangular boxes with a title and a content. What would be a better way to implement this? i am planning to use partial views. And then call Html.renderaction on the aggregate view. Is this a good choice or is there a better way to do it?

2.)Also when the widget encounters any exception i would like to show a custom error message just on the widget area. I dont want the entire page to be redirected to an error page. Just the rectangle area alone.

Upvotes: 2

Views: 1282

Answers (1)

JOBG
JOBG

Reputation: 4624

@Html.RenderAction should do the work, for the exceptions a try/catch can help you:

[ChildActionOnly]
public ActionResult Widget(int id) {
    try
    {
      var widget = Repository.GetWidget(id);
      return PartialView(widget);
    }
    catch
    {
      return PartialView("WidgetErrorPage");
    }
}

UPDATE: In that case you can use an ActionFilter to handle the exceptions, like explained here Return View from ActionFilter or here Returning a view with it's model from an ActionFilterAttribute:

public class WidGetHandleException : ActionFilterAttribute
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;
            filterContext.Result = new PartialViewResult
            {
                ViewName = "WidgetErrorPage",
                ViewData = filterContext.Controller.ViewData,
                TempData = filterContext.Controller.TempData,
            };
        }
    }

And then decorate all your widget actions like this:

 [ChildActionOnly]
 [WidGetHandleException]
 public ActionResult Widget() 
 {

 }

Upvotes: 1

Related Questions