2016-07-26 3 views
1

2つのHtml.DropDownListをRazorで作成しました。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); 

2番目の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 resultメソッドを作成しました。

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. 
    } 

方法SubCategoryとしてSelectList.DataValueField、代わりのIdを設定するには?

+0

あなたの質問は不明です。選択リストの値フィールドをSubCategoryにすることはどういう意味ですか? SelectListはすでにSubCategoryのリストである 'subs'によって生成されていますか?テキストフィールドをどのように設定しますか? –

+0

SelectListのデータ値フィールドは、テキストフィールドの値に対応する必要があります。データフィールドがSubCategoryの場合、どのテキスト値が対応しますか? –

+0

私はこの新しいSelectListのようなものをペーストしたい(サブ、 "SubCategory"、 "Subcategory")。ドロップダウンリストのすべてのオプションを使うとコントローラにSubCategoryオブジェクトの代わりにフォームを送信するときにIdだけをコントローラに送信する。コントローラは、値の代わりに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 
} 

あなたは新製品のサブカテゴリがポストバック時にnullになります作成しても、あなたが選択したサブカテゴリを割り当てるためにIDを使用するときだと。

エンティティの編集画面にある場合は、現在選択されているサブカテゴリを表示するためにidを使用します。 IDを取得して、選択リストの選択した値プロパティで使用します。

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

はい、私は知っているとして、プロパティやクラスのフィールドではなく、クラス全体ではありません。サブコレクション内のクラス全体で作成する必要があります。 –

+0

ああ申し訳ありませんが、私はあなたに誤解しています。 –

+0

私はそれを得ました、最良の方法はidに値を格納することです、そしてidによってはコレクション内のいくつかのサブカテゴリを見つけることです。コントローラ右ですか? –

関連する問題