Search code examples
asp.net-mvcvisual-studiodisplay-templates

How to configure an MVC dropdown depending on which view calls it


I have two views, BatchReceipt and Receipt which utilise the same model. Until now they have used the same display template of ReceiptType. But I want to have one exclude certain items and the other to have the full list (so essentially a second .cshtml display template called ReceiptTypeFull). How do I configure each of these views in Visual Studio to utilise the different Display Templates?

Some additions to show the code being used:

I have file ReceiptType.cshtml being used as a DisplayTemplate which contains the following to setup the receipt dropdown

@using Clinton.Web.Helpers.EnumHelpers
@{
    var item = EnumsHelper.GetNameFromEnumValue(Model);
 }

I want to use a different DisplayTemplate, call it ReceiptTypeFull.cshtml

@using Clinton.Web.Helpers.EnumHelpersFull
@{
    var item = EnumsHelper.GetNameFromEnumValue(Model);
 }
@item

The difference is in calling the enumhelper or the enumhelperfull to vary the query populating the dropdown. My problem is that I cannot see how to redirect the view to use the different enumhelper/displaytemplate/

Thanks


Solution

  • I think I understand what you are getting at. You want to control which template is used for an Enum in the view.

    I will explain using editor templates but it works the same way if you use display templates. You should be able to follow and apply for your scenario.

    The idea is to use this overload of the editor html helper.

    public static MvcHtmlString Editor(this HtmlHelper html, string expression, string templateName);
    

    It is called like this

    @Html.Editor("{property name}", "{template name}").
    

    Below is an example to show it being used.

    Suppose we have this enum

    public enum MyItems
    {
    
        Item1 = 1,
        Item2 = 2,
        Item3 = 3  
    }
    

    This helper

    public static class MyEnumHelper
    {
        public static List<MyItems> GetAllItems()
        {
             return new List<MyItems>()
             {
                  MyItems.Item1,
                  MyItems.Item2,
                  MyItems.Item3
              };
         }
    
          public static List<MyItems> GetSomeItems()
          {
              return new List<MyItems>()
              {
                  MyItems.Item1,
                  MyItems.Item2                
               };
          }
    }
    

    This controller

    public class HomeController : Controller
    {      
        public ActionResult AllItems()
        {
            return View();
        }
    
        public ActionResult SomeItems()
        {
            return View();
        }
    }
    

    We have these 2 editor templates, which are put in views/shared/editortemplates

    First one called MyItems.cshtml which is the all one

    @model MyItems?
    
     @{
      var values = MyEnumHelper.GetAllItems().Cast<object>()
          .Select(v => new SelectListItem
          {
             Selected = v.Equals(Model),
             Text = v.ToString(),
             Value = v.ToString()
           });
       }
    
    @Html.DropDownList("", values)  
    

    Second one called MyItems2.cshtml which is the some one

    @model MyItems?
    
    @{
        var values = MyEnumHelper.GetSomeItems().Cast<object>()
            .Select(v => new SelectListItem
            {
                Selected = v.Equals(Model),
                Text = v.ToString(),
                Value = v.ToString()
            });
    }
    
    @Html.DropDownList("", values)  
    

    Then in the AllItems.cshtml to get the MyItems.cshtml template called we need

    @model MyItemsViewModel  
    
    @using (Html.BeginForm())
    {    
        @Html.EditorFor(x => x.MyItem)    
        <submit typeof="submit" value="submit"/>
    }
    

    And in the SomeItems.cshtml to get some of the items by calling MyItems2.cshtml we use

    @model MyItemsViewModel
    
    @using (Html.BeginForm())
    {    
        @Html.Editor("MyItem", "MyItems2")   @* this bit answers your question *@
        <submit typeof="submit" value="submit" />
    }