2012-04-23 91 views
0

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

这就是我对视图模型:

[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 当它应该是:实习

回答

1

你可以做一个简单的服务,将返回该字符串,然后使用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); } 
    ); 
}); 
+0

谢谢你的提示我一直记在上面,但我的目的,这将是一个周围很多改变。我想要更多基于我拥有的解决方法。谢谢 – Masriyah 2012-04-23 20:06:29

0

另一种办法是使自己的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() }); 

在View:

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