简体   繁体   English

mvc3 checkboxfor枚举,模型中没有枚举

[英]mvc3 checkboxfor enum without enum being in model

Problem: 问题:

I want to display a list of checkboxes based on enums: eg item1[] item2[] item3[] 我想显示一个基于枚举的复选框列表:例如item1 [] item2 [] item3 []

However these checkboxes need to be checked based on the contents of a comma seperated list model.platforms 但是,需要根据逗号分隔列表model.platforms的内容来选中这些复选框。

For example: If model.platforms = "1,3,5", then the output on the page should be: 例如:如果model.platforms =“ 1,3,5”,则页面上的输出应为:

item1[x] item2[] item3[x] item4[] item5[x] item1 [x] item2 [] item3 [x] item4 [] item5 [x]

What I have done so far: So far I have tried creating a custom helper which takes in the enum type and my "model.platforms. The aim is to split the platforms at the comma and then when iterating through the enums, check the checkbox if the value for the enum is in the list. 到目前为止,我已经完成了什么:到目前为止,我已经尝试创建一个自定义帮助程序,该服务程序将枚举类型和我的“ model.platforms”都使用。目标是在逗号处分割平台,然后在枚举进行迭代时,选中复选框如果枚举的value在列表中。

Am I even approaching this right? 我什至接近这个权利吗? I know my way around MVC/C# but these helpers really are new to me. 我知道我围绕MVC / C#的方式,但是这些助手对我来说真的是新手。 Any guidance to point me in the right direction would be appreciated 任何指向我正确方向的指导将不胜感激

I have tried to look up various solutions on SO, but most of them revolve around the enums being part of the model which isn't practical for me due to the way the model is generated(and I really don't want to add a view model for this particular scenario) 我曾尝试在SO上查找各种解决方案,但是大多数解决方案都是围绕作为模型一部分的枚举展开的,由于模型的生成方式,这对于我来说并不实用(我真的不想添加一个此特定方案的视图模型)

Code so far 到目前为止的代码

--helper.cs-- --helper.cs--

public static MvcHtmlString PlatformList<enumType>(this HtmlHelper htmlHelper, string platformList)
{
  List<string> selectedPlatforms = platformList.Split('/').ToList(); //This will come from model.platforms ("platform1/platform2/etc")
  TagBuilder tg = new TagBuilder("div");
  string innerhtml = "";
  foreach (var item in Enum.GetValues(typeof(enumType)))
  {

    innerhtml = innerhtml + "{build up here}";

  }
  tg.SetInnerText(innerhtml);
  return new MvcHtmlString(innerhtml);
}

--Enum.cs-- --Enum.cs--

public enum en_GamePlatforms
{
  [Display(Name = "PC")]
  PC =1,
  [Display(Name = "DOS")]
  DOS = 2,
  ...{etc}...
  }

--View-- - 视图 -

   @(Html.Raw(Html.PlatformList<MagpieEnumerations.en_GamePlatforms>(Model.Platform).ToString()))

Below is my original approach which i really don't like, but provides the correct output on the page.(but does it off enum name rather than value :()) 下面是我真的不喜欢的原始方法,但是在页面上提供了正确的输出。(但是它是用枚举名称而不是值:()来完成的)

 @{
                List<string> platformsActive = Model.Platform.Split(',').ToList();

            }
            @foreach (myenums.en_GamePlatforms item in Enum.GetValues(typeof(myenums.en_GamePlatforms)))
            {
                <span>
                    @Html.Label("SelectedPlatform" + item, Enum.GetName(typeof(myenums.en_GamePlatforms), item))
                    @{bool t = platformsActive.Contains(item.ToString(Model));}
                    <input name="Selectedtypes" id="Selectedtypes" type="checkbox"  @(t ? "checked='checked'" : "")  value="@item"/> | 
                </span>
            }

You can achieve it by using following code. 您可以使用以下代码来实现。

public static IHtmlString CheckboxListForEnum<T>(this HtmlHelper html, string name, T modelItems) where T : struct
{
    StringBuilder sb = new StringBuilder();

    foreach (T item in Enum.GetValues(typeof(T)).Cast<T>())
    {
        TagBuilder builder = new TagBuilder("input");
        long targetValue = Convert.ToInt64(item);
        long flagValue = Convert.ToInt64(modelItems);

        if ((targetValue & flagValue) == targetValue)
            builder.MergeAttribute("checked", "checked");

        builder.MergeAttribute("type", "checkbox");
        builder.MergeAttribute("value", item.ToString());
        builder.MergeAttribute("name", name);
        builder.InnerHtml = item.ToString();

        sb.Append(builder.ToString(TagRenderMode.Normal));
    }

    return new HtmlString(sb.ToString());
}

Please refer this url checkboxlist in asp.net MVC 在asp.net MVC中参考此URL 复选框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM