繁体   English   中英

显示字符串文本而不是项目ID

[英]display string text rather than item id

我想显示一个从下拉列表中选择的项目。 到目前为止,我只获得从以前的下拉列表中选择的项目的ID显示。 我想获取项目的文本/描述名称,而不是其ID号。

这是我对viewModel的:

[LocalizedDisplayName("BillingRate", NameResourceType = typeof(User))]
    public short BillingRateId { get; set; }

    [UIHint("DropDownList")]
    [DropDownList(DropDownListTargetProperty = "BillingRateId")]
    public IEnumerable<SelectListItem> BillingRates { get; set; }

这是我为.ascx表单页面所拥有的:

<%:Html.LabelFor(m => m.BillingRateId)%>
<%:Html.EditorFor(m => m.BillingRateId, Model.BillingRates)%>
<%:Html.ValidationMessageFor(m => m.BillingRateId)%>

当我运行并查看页面时,我会在说明框中进入:4应该是:实习

您可以提供一个简单的服务,该服务仅返回该字符串,然后使用jQuery AJAX进行填充。

public ContentResult GetBillingRate(int id)
{
    //get your billing rate
    return this.Content(billing_rate_string, "text/plain");
}

然后在您的JavaScript中:

$('#BillingRateId').change(function() {
    $.get('@Url.Action("GetBillinRate", "YourController")/' + $(this).val(),
        function(data) { $('#element_you_want_it_to_show_in').html(data); }
    );
});

另一种方法是使您自己的IEnumerable并将其推入DropDownList。

在您的控制器中:

// this is assuming you can get objects with both name/id for your billing rates 
ViewBag.BillingRates = Db.GetBillingRatesAndNames()
    .Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() });

在视图中:

<%:Html.EditorFor(m => m.BillingRateId, 
    (IEnumerable<SelectListItem>)ViewBag.BillingItems)%>

暂无
暂无

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

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