2012-04-23 9 views
0

以前に選択したアイテムをドロップダウンリストから表示したかったのです。私が今まで持っているものでは、以前にドロップダウンから選択されたアイテムのidだけが表示されます。 ID番号ではなくアイテムのテキスト/説明名を取得したいと思います。アイテムIDではなく文字列テキストを表示

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

私は実行して、ビューのページ私が説明に取得する:これは私がた.ascxフォームページのために持っているものである

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

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

この

は私がViewModelにのために持っているものですボックス:4 する必要があります:インターンシップ

答えて

1

jQuery AJAXを使用して、その文字列を返すシンプルなサービスを作成できます。次に、あなたのJavaScriptで

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

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

感謝。私は、私が持っているものに基づいてより多くの回避策を望んでいました。ありがとう – Masriyah

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() }); 

ビューで:私はそれではなく、周りの変更の多くになります私の目的のためにメモしておく先端ため

<%:Html.EditorFor(m => m.BillingRateId, 
    (IEnumerable<SelectListItem>)ViewBag.BillingItems)%> 
関連する問題