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.对不起,因为我的英语不好
我undestand。以及我如何添加make.NameBrand添加在表上Make on View Create? – dev85 2012-03-01 09:46:52