0

I need to create a custom @Html.Partial() method.

Use Case

I have a .cshtml page where in I have multple sections like below

<!-- EDUCATION -->
@Html.Partial("Templates/Create/Modules/Education")

<!-- JOBS -->
@Html.Partial("Templates/Create/Modules/Jobs")

I want to be able to create a custom .Partial() method. Something on the likes of this

@Html.CustomPartial("Templates/Create/Modules/Jobs", "jobs", "edit")

where in the last two parameters are module id and action type id respectively. Using these values I will make a decision in my CustomPartial what I need to show in the output.

I am not sure how to go about this one. Please advice.

Or if someone can point me to the source code of the Html.Partial that too would be very helpful.

0

3 Answers 3

2

You can already do this using the overload of @Html.Partial() that accepts a ViewDataDictionary

@Html.Partial("Templates/Create/Modules/Jobs", new ViewDataDictionary { { "module", someValue }, {"edit", anotherValue }})

Then in the partial

@if(ViewData["module"] == someValue)
{
  // do something
}
else
{
  // do something else
}

And if your still interested, here is the source code

Sign up to request clarification or add additional context in comments.

Comments

0

Here is what worked for me

public static class CustomHtmlHelpers
{
    public static MvcHtmlString RenderModule(this HtmlHelper helper,
                                             string partialViewName, 
                                             string moduleName,
                                             string actionType)
    {
        var isAccessAllowed = _someService.someMethod(userId, moduleName, actionType);    
        if (isAccessAllowed)
        {
            return helper.Partial(partialViewName);
        }
        else
        {
            return MvcHtmlString.Empty;
        }
    }
}

2 Comments

If you intend to do it this way (that sort of logic does not belong in a html helper), why not use @Html.Action() which calls a child action method where you can perform the validation in the controller and return a corresponding view?
Nice. Thanks again, will think about this.
0
      @Html.Partial("../Partial_views/_Menu_creation", new ViewDataDictionary { { "Type", 
     "Menu" }, { "Menu", "Dimensions" }, { "Active", "Yes" }, { "Icon", "icon-1" }, { 
     "data-hide", "" } })

In partial view
      @if (ViewData["Type"] == "Menu")
      {
         @if (ViewData["Active"] == "Yes")
          {
             <a data-check='@ViewData["Menu"]' role="button" class="active-menu">
               <b class='@ViewData["Icon"]'></b>
               <span>@ViewData["Menu"]</span>
            </a>
       }
       else
       {



    }
     }
     @if (ViewData["Type"] == "Heading")
     {
     }

This is working

1 Comment

Can you update the formatting and give an explanation of what you changed to help OP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.