简体   繁体   English

MVC.NET:为DropDownList定义强类型HTML帮助器

[英]MVC.NET: define a strongly typed HTML Helper for DropDownList

I am trying to find out how I could define my own HtmlHelper for creating a dropdownlist. 我试图找出如何定义自己的HtmlHelper来创建下拉列表。 I have the following scenarion: I am using the select2 jQuery plugin ( http://ivaynberg.github.io/select2/ ) for multiple selects with search and better interface. 我有以下场景:我将select2 jQuery插件( http://ivaynberg.github.io/select2/ )用于具有搜索和更好界面的多项选择。 My Model has x id's of tags associated. 我的模型具有x个ID的关联标签。 As I understand it, these tags can only get a value when an Html helper like @Html.DropDownListFor() is used? 据我了解,仅当使用@ Html.DropDownListFor()之类的HTML帮助程序时,这些标签才能获得值。 I would like to 我想要

  • write pure html and somehow tell it the value has to be stored in the list of tag-id's in my model 写纯HTML并以某种方式告诉它值必须存储在我的模型的tag-id列表中

    OR 要么

  • make an HTML Helper which can achieve the same effect. 制作可以实现相同效果的HTML Helper。

Currently I have this, but I see no way of passing the selected values to the model when the form gets posted: 目前,我已经有了这个,但是在表单发布时,我看不到将所选值传递给模型的方法:

<div>
        <label>Kies je thema's (maximum 2):</label>
        <select id="select2select" multiple style="width: 500px">
            @foreach (var top in ((List<Topic>)ViewBag.TopIds).Where(top => top.MainTopic == null))
            {
                <option value="@top.TopicId" class="optionGroup">@top.Name</option>
                foreach (var subTopic in top.SubTopics)
                {
                    <option value="@subTopic.TopicId" class="optionChild">@subTopic.Name</option>
                }
            }
        </select>
    </div>

How can I get the selected value in the property in my list while keeping this structure? 如何在保持此结构的同时在列表的属性中获取选定的值?

Extra: as you can see my options have classes, this is because I have Main tags and subtags. 附加:如您所见,我的选项有类,这是因为我有Main标签和subtags。 Users can choose either main tags or subtags, so I can't work with <optiongroup> because they 'groups' have to be selectable. 用户可以选择主标签或子标签,因此我不能使用<optiongroup>因为它们的“组”必须是可选的。 I give them, depending on their class, a different format using select2 format. 根据他们的班级,我给他们提供了使用select2格式的不同格式。

请尝试以下代码以纠正问题

 @Html.DropDownListFor(m => top.SubTopics, new SelectList(top.SubTopics, "TopicId", "Name"))

There is a couple of ways you can do this. 您可以通过两种方法来执行此操作。

@Html.DropDownListFor(m => top.SubTopics, new SelectList(top.SubTopics, "TopicId", "Name"))

@Html.DropDownListFor(m => Model.SubTopics, Model.SubTopics, null, htmlAttributes: new { @class = "ui search selection dropdown clearable", @clearable = "", @id = "Model_SubTopics" })

These two can have issues with changing html Attribute "name" values for different model binding. 对于不同的模型绑定,这两个更改html属性“名称”值可能会有问题。

you can use a Tag Helper to do it the more modern way. 您可以使用Tag Helper以更现代的方式进行操作。

<select asp-for="Model.SubTopics" asp-items="@Model.SubTopics"  class="ui search selection dropdown clearable" ></select>

This way will take the pre-configured value if you have model binding for the input form. 如果您具有输入表单的模型绑定,则这种方式将采用预先配置的值。

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

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