2011-02-26 78 views
3

我有兩個類Entry和Paradigm。 Entry類具有ParadigmId和Paradigm屬性。所以在我看來我有@Model.Entry.Paradigm。我如何構建一個使用Paradigm模型的新語法的DropDownListFor?ASP.NET MVC DropDownListFor與模型中的嵌套屬性

// Entry Model 
    [Bind(Exclude = "EntryId")] 
    public class Entry 
    { 
     [ScaffoldColumn(false)] 
     public int EntryId { get; set; } 
. 
     [Display(Name = "Type")] 
     public int ParadigmId { get; set; } 

     public virtual Paradigm Paradigm { get; set; } 
    } 

// Paradigm Model 
public class Paradigm 
{ 
    [ScaffoldColumn(false)] 
    public int ParadigmId { get; set; } 

    [Required] 
    public string Name { get; set; } 

    public List<Entry> Entries { get; set; } 
} 

在我看來,我有@Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)。但是該模型是Paradigm類型,不是IEnumerable。由於Paradigm是我的類的一部分(對於Entity Framework Code First),我不需要使用大多數示例中列出的單獨的ViewData/ViewBag。

我搜索了一下,看到使用Helper/Extension方法的人將模型轉換爲SelectList。在我的模型中使用DropDownListFor的最佳方式是什麼?

@* Create View *@ 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.Entry.ParadigmId) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm) 
     @Html.ValidationMessageFor(model => model.Entry.ParadigmId) 
    </div> 

回答

1

您的鏈接Entry.Paradigm懶加載單個Paradigm,外鍵引用的一個。它不會加載數據庫中的所有Paradigm。

如果你想有一個所有範例的下拉列表,綁定到所選範例。然後,您將需要一個單獨的ViewBag或Model屬性,其中包含它們全部的列表。

0

我一直在使用:

public abstract class DropdownVm 
{ 
    /// <summary> 
    /// Set up a dropdown with the indicated values 
    /// </summary> 
    /// <param name="value">the current value, for determining selection</param> 
    /// <param name="options">list of options to display</param> 
    /// <param name="prependLabelAndValues">list of alternating label/value entries to insert to the beginning of the list</param> 
    public List<SelectListItem> SetDropdown<T>(T value, IEnumerable<KeyValuePair<T, string>> options, params object[] prependLabelAndValues) 
    { 
     var dropdown = options.Select(o => new SelectListItem { Selected = Equals(o.Key, value), Value = o.Key.ToString(), Text = o.Value }).ToList(); 

     // insert prepend objects 
     for (int i = 0; i < prependLabelAndValues.Length; i += 2) 
     { 
      dropdown.Insert(0, new SelectListItem { Text = prependLabelAndValues[i].ToString(), Value = prependLabelAndValues[i + 1].ToString() }); 
     } 

     return dropdown; 
    } 
} 

/// <summary> 
/// ViewModel with a single dropdown representing a "single" value 
/// </summary> 
/// <typeparam name="T">the represented value type</typeparam> 
public class DropdownVm<T> : DropdownVm 
{ 
    /// <summary> 
    /// Flag to set when this instance is a nested property, so you can determine in the view if `!ModelState.IsValid()` 
    /// </summary> 
    public virtual bool HasModelErrors { get; set; } 

    /// <summary> 
    /// The user input 
    /// </summary> 
    public virtual T Input { get; set; } 

    /// <summary> 
    /// Dropdown values to select <see cref="Input"/> 
    /// </summary> 
    public virtual List<SelectListItem> Dropdown { get; set; } 

    /// <summary> 
    /// Set up <see cref="Dropdown"/> with the indicated values 
    /// </summary> 
    /// <param name="availableOptions">list of options to display</param> 
    /// <param name="prependLabelAndValues">list of alternating label/value entries to insert to the beginning of the list</param> 
    public virtual void SetDropdown(IEnumerable<KeyValuePair<T, string>> availableOptions, params object[] prependLabelAndValues) 
    { 
     this.Dropdown = SetDropdown(this.Input, availableOptions, prependLabelAndValues); 
    } 

    public override string ToString() 
    { 
     return Equals(Input, default(T)) ? string.Empty : Input.ToString(); 
    } 
} 
您與創建

var vm = new DropdownVm<string>(); 
vm.SetDropdown(new Dictionary<string, string> { 
    { "option1", "Label 1" }, 
    { "option2", "Label 2" }, 
}, "(Choose a Value)", string.Empty); 

,或者更具體地說,你的情況:

var models = yourDataProvider.GetParadigms(); // list of Paradigm 
var vm = new DropdownVm<int>(); 
vm.SetDropdown(
    models.ToDictionary(m => m.ParadigmId, m => m.Name), 
    "(Choose a Value)", string.Empty 
); 

而且與視圖渲染:

<div class="field"> 
    @Html.LabelFor(m => m.Input, "Choose") 
    @Html.DropDownListFor(m => m.Input, Model.Dropdown) 
    @Html.ValidationMessageFor(m => m.Input) 
</div>