2012-03-01 7 views
0

ビューで[作成]をクリックします。新しい商品を追加します。ドロップダウンリストからカテゴリ名を選択する必要があります。問題は、私はテーブルの商品カテゴリIDのみを追加することです。そして私はどのようにカテゴリを追加して名前を付けますか?両方にデータを追加する方法:Html.LabelForからСategoryidとName Category?

構造私のDB:

表ブランド:idbrand、名前。

テーブルメイク:idbrand、namebrand、idmake、name、price、urlmake。

I次の操作を行います。

// GET: /ShopManager/Create 

    public ActionResult Create() 
    { 
     ViewBag.BrandID = new SelectList(db.Brand, "BrandID", "Name"); 
     return View(); 
    } 

    // 
    // POST: /ShopManager/Create 

    [HttpPost] 
    public ActionResult Create(Make make) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Make.AddObject(make); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.BrandID = new SelectList(db.Brand, "BrandID", "Name", make.BrandID); 
     return View(make); 
    } 



    @using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
    <legend>Марка телефона</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.BrandID, "Бренд") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("BrandID", String.Empty) 
     @Html.ValidationMessageFor(model => model.BrandID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Name, "Марка телефона") 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Name) 
     @Html.ValidationMessageFor(model => model.Name) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Price, "Цена") 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Price) 
     @Html.ValidationMessageFor(model => model.Price) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.UrlMake, "Изображение") 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.UrlMake) 
     @Html.ValidationMessageFor(model => model.UrlMake) 
    </div> 

    <p> 
     <input type="submit" value="Создать" /> 
    </p> 
    </fieldset> 


    <div> 
    @Html.ActionLink("Back to List", "Index") 
    </div> 

私はブランドのBrandIDと名(カテゴリ名)を作成し、表に追加する方法は?助けてください。

P.S.私の悪い英語には申し訳ありません

答えて

1

ブランドテーブルがあるので、ブランド名をMakeにも保存する必要はありません。単純な結合でそれを得ることができます。とにかく実際に作成する場合は、次のコードとして設定することができます。

make.NameBrand = db.Brand.Where(b => b.idbrand == make.idbrand).SingleOrDefault().namebrand; 
+0

私はundestandです。そして、私はどのようにしてmake.NameBrandを追加しますか? – dev85

関連する問題