2016-07-26 21 views
1

我用Razor創建了2個Html.DropDownList。如何將SelectList.DataValueField設置爲List的整個對象?

<p>Category:</p> 
     @Html.DropDownList("Category", new SelectList(catList, "Id", "category"), "Select category"); 
     <p>SubCategory:</p> 
     @Html.DropDownListFor(x=>x.SubCategory, new SelectList(string.Empty, "Id", "Subcategory"), "Select sub category",new {id="subCategory" }); 
     @Html.ValidationMessageFor(x => x.SubCategory); 

第二個DropDownListFor的模型看起來像這樣。

namespace OnlineShop.Models 

{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    public partial class Product 
    { 
     public int Id { get; set; } 
     [Required(ErrorMessage = "Enter name of product")] 
     public string Name { get; set; } 
     [Required(ErrorMessage = "Enter article of product")] 
     public string Article { get; set; } 
     [Required(ErrorMessage = "Enter description of product")] 
     public string Description { get; set; } 
     [Required(ErrorMessage = "Enter price of product")] 
     public decimal Price { get; set; } 
     [Required(ErrorMessage = "Select picture of product")] 
     public byte[] Picture { get; set; } 
     [Required(ErrorMessage = "Enter date of product")] 
     public System.DateTime DateAdded { get; set; } 
     [Required(ErrorMessage = "Choose subcategory of product")] 
     public virtual SubCategory SubCategory { get; set; }//property that i want to associate with SelectList.DataValueField. 
    } 
} 

然後我創建了JSon結果方法。

public JsonResult GetSubs(int category) 
    { 
     List<SubCategory> subs = goods.SubCategorySet.Where(x=>x.Category.Id==category).ToList(); 
     return Json(new SelectList(subs, "Id", "Subcategory")); //here is row of code that i want to change. 
    } 

如何設置SelectList.DataValueFieldSubCategory,而不是Id

+0

你的問題不清楚。你是什​​麼意思使選擇列表的值域是子類別? SelectList已經由'subs'生成,它是一個SubCategory類型的列表?你會如何設置文本框? –

+0

SelectList中的數據值字段應該對應於文本字段的值。所以如果你的數據字段是SubCategory,那麼對應的文本值是什麼? –

+0

我想粘貼一些像這樣新的SelectList(subs,「SubCategory」,「Subcategory」)。在dropdownlist中包含所有選項,當我提交表單時,只發送Id到控制器,而不是SubCategory對象到controller。這就是爲什麼在控制器我在Product.SubCategory中獲取null而不是值。 –

回答

1

我很確定你不能在下拉菜單中加載子類別。但是,您可以從下拉列表中保存ID,並使用該ID從數據庫中檢索相應的項目,然後將其添加到實體中。

您可以在您的操作參數列表中使用與下拉列表相同的名稱和類型創建參數以檢索值。

@Html.DropDownList("SubCategoryID", ViewBag.Subs, "Select an Option", new { @class = "pretty" }) 

[HttpPost] 
public ActionResult Index(Product model, int SubCategoryID) 
{ 

    //here you can retrieve your item from the database using the value in 
    //SubCategoryID 
    //Psuedo code below: 

    Subcategory test = goods.SubCategorySet.Where(sc => sc.id == SubCategoryID); 

    model.SubCategory = test; 

    //save/update model here 
} 

由於您創建新產品子類別在回發後將爲空,但這是您使用ID分配所選子類別時的情況。

如果您在實體的編輯屏幕上,您還將使用該ID來顯示當前選定的子類別。只需獲取id並在選擇列表的選定值屬性中使用它。

new SelectList(subs, "Id", "Subcategory", SubCategoryID)); 
+0

是的,但據我所知,值可以是任何財產或類的領域,但不是整個class.I需要在整個類的收集。 –

+0

哦,我很抱歉,我誤解你。我不會建議加載整個類實例在下拉無論如何只是因爲我會用適當的解決方案編輯答案 –

+0

好吧,我明白了,最好的方法是將值存儲在id中,而不是通過id在集合中查找某個子類別。管制員對嗎? –

相關問題