2012-03-01 55 views
0

在視圖中創建我想要添加新產品。我需要從下拉列表中選擇類別名稱。問題是,我在表格產品中只添加了類別ID。以及我如何添加並命名類別?如何將數據添加到兩個:從一個Html.LabelFor的類別ID和名稱類別?

結構我的DB:

表品牌:idbrand,名稱。

表製作:idbrand,namebrand,idmake,名稱,價格,urlmake。

我做到以下幾點:

// 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和Name(類別名稱)?請幫忙。

P.S.對不起,因爲我的英語不好

回答

1

既然你有一個品牌表,沒有必要在品牌也存儲品牌的名字,你可以通過一個簡單的加入。無論如何,如果你真的想在創建,你可以將其設置爲以下代碼

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

我undestand。以及我如何添加make.NameBrand添加在表上Make on View Create? – dev85 2012-03-01 09:46:52

相關問題