Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an empty viewcomponent MVC 6?

I have searched but I did not find any way to return an empty IViewComponentResult. The only way I managed to do it is by returning an empty View. Is there a better way? This is my code:

public class ClientNavigationViewComponent : ViewComponent
{
    public IViewComponentResult Invoke()
    {
        return User.IsInRole(UserRoles.CLIENT)
            ? View("_ClientMenu")
            : (IViewComponentResult)new EmptyResult();
    }
}

This is the exception:

An exception of type 'System.InvalidCastException' occurred in but was not handled in user code

Additional information: Unable to cast object of type 'Microsoft.AspNet.Mvc.EmptyResult' to type 'Microsoft.AspNet.Mvc.IViewComponentResult'.

I have tried to return null but that won't work too. Any ideas? EDIT Made it work like this:

public class ClientNavigationViewComponent : ViewComponent
{
    public IViewComponentResult Invoke()
    {
        if (User.IsInRole(UserRoles.CLIENT))
            return View("_ClientMenu");
        return new EmptyViewComponent();
    }
}

public class EmptyViewComponent : IViewComponentResult
{
    public void Execute(ViewComponentContext context)
    {
    }

    public Task ExecuteAsync(ViewComponentContext context)
    {
        return Task.FromResult(0);
    }
}
like image 288
KangJiYoung13 Avatar asked Mar 10 '16 13:03

KangJiYoung13


People also ask

How do I create a simple view component?

Walkthrough: Creating a simple view component Add a ViewComponent class. View component classes can be contained in any folder in the project. Because the class name... Create the view component Razor view. Create the Views/Shared/Components folder. This folder must be named Components. Specifying a ...

How do I invoke a view component from a controller method?

View components are typically invoked from a view, but you can invoke them directly from a controller method. While view components don't define endpoints like controllers, you can easily implement a controller action that returns the content of a ViewComponentResult. In this example, the view component is called directly from the controller:

How does a view component handle a request?

A view component defines its logic in an InvokeAsync method that returns a Task<IViewComponentResult> or in a synchronous Invoke method that returns an IViewComponentResult. Parameters come directly from invocation of the view component, not from model binding. A view component never directly handles a request.

How to return content from iviewcomponentresult?

public IViewComponentResult Invoke () { if (User.IsInRole (UserRoles.CLIENT)) return View ("_ClientMenu"); return Content (string.Empty); } A property can inherit from an interface not cast to it. Try create an object which is inherits from IViewComponentResult. then return this object. It should work.


1 Answers

You can do the following :

public IViewComponentResult Invoke()
{
  if (User.IsInRole(UserRoles.CLIENT))
     return View("_ClientMenu");

  return Content(string.Empty);
}
like image 200
david laporte Avatar answered Sep 21 '22 13:09

david laporte