使用C#的多个值的SelectList

时间:2015-10-23 12:05:44

标签: c# asp.net-mvc

我需要使用以下值构建一个选择列表:

<ul id="sch-brand-dropdown">
    <li data-id="1" data-id2="11">text1</li>
    <li data-id="2" data-id2="12">text2</li>
    <li data-id="3" data-id2="13">text3</li>
</ul>

对于一个值,我使用以下方法:

public static IEnumerable<SelectListItem> DropDownList()
{
    var brandServices = new BrandService();

    var brandsDTO = brandServices.GetAll().OrderBy(b => b.Name);

    var brandsList = brandsDTO.Select(brand =>
        new SelectListItem
        {
            Value = brand.Id.ToString(),
            Text = brand.Name
        });

    return brandsList;
}

基于品牌模型:

[Table("Brands")]
public class Brand
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

我在这个视图中出现:

<ul id="sch-brand-dropdown">
    @foreach (var brand in Model.BrandList)
    {
        <li data-id="@brand.Value">@brand.Text</li>
    }
</ul>

3 个答案:

答案 0 :(得分:3)

修改您的模型,如

[Table("Brands")]
public class Brand
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Value1 { get; set; }
    public int Value2 { get; set; }
}

并使用

<ul id="sch-brand-dropdown">
    @foreach (var brand in Model.BrandList)
    {
        <li data-id="@brand.Value1" data-id2="@brand.Value2">@brand.Text</li>
    }
</ul>

答案 1 :(得分:1)

可能更改DropDownlist方法的返回类型:

public static IEnumerable<MultValoredItem> DropDownList()
{
    var brandServices = new BrandService();

    var brandsDTO = brandServices.GetAll().OrderBy(b => b.Name);

    var brandsList = brandsDTO.Select(brand =>
    new MultValoredItem
    {
        Value1 = brand.Id.ToString(),
        Value2 =  //Another value here
        Text = brand.Name
    });

    return brandsList;
}

public class MultValoredItem
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }
    public string Text { get; set; }
}

<ul id="sch-brand-dropdown">
  @foreach (var brand in Model.BrandList)
  {
    <li data-id1="@brand.Value1" data-id2="@brand.Value2">@brand.Text</li>
  }
</ul>

答案 2 :(得分:0)

也许这不是最佳选择,但您可以使用转发器。或者直接使用HTML代码直接使用文字。

课程:

<asp:Repeater ID="Repeater" runat="server" >
  <ItemTemplate>
    <li data-id="<%# Eval("id")  %>" data-id2="<%# Eval("idCATEGORY")  %>"><%# Eval("text")  %></li>
</ItemTemplate>
</asp:Repeater>

转发器:

 Repeater.DataSource = data_Brand;
 Repeater.DataBind();

在page_load中使用repeater:

<ul><li>

或字面意思 - &gt;然后在c#中为 <pm:content> <script> jQuery(document).ready(function() { 'use strict'; var videoElement = document.querySelector('video'); var videoSelect = document.querySelector('select#videoSource'); navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; function gotSources(sourceInfos) { for (var i = 0; i !== sourceInfos.length; ++i) { var sourceInfo = sourceInfos[i]; var option = document.createElement('option'); option.value = sourceInfo.id; if (sourceInfo.kind === 'audio') { } else if (sourceInfo.kind === 'video') { option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1); videoSelect.appendChild(option); } else { console.log('Some other kind of source: ', sourceInfo); } } } if (typeof MediaStreamTrack === 'undefined' || typeof MediaStreamTrack.getSources === 'undefined') { alert('This browser does not support MediaStreamTrack.\n\nTry Chrome.'); } else { MediaStreamTrack.getSources(gotSources); } function successCallback(stream) { window.stream = stream; // make stream available to console videoElement.src = window.URL.createObjectURL(stream); videoElement.play(); } function errorCallback(error) { console.log('navigator.getUserMedia error: ', error); } function start() { videoElement = document.querySelector('video'); if (!!window.stream) { videoElement.src = null; window.stream.stop(); } var videoSource = videoSelect.value; var constraints = { audio: false, video: { optional: [{ sourceId: videoSource }] } }; navigator.getUserMedia(constraints, successCallback, errorCallback); } videoSelect.onchange = start; start(); }); </script> <p:outputLabel value="Seleccione Camara:" /> <select id="videoSource"></select> <p:photoCam widgetVar="pc" listener="#{eventoMB.oncapture}" update="photo" /> 创建完整的字符串?的foreach?而?和文字内页。