C# 如何从一个Html.LabelFor向Category id和Name Category添加数据?
在视图创建中,我想添加一个新产品。我需要从下拉列表中选择类别名称。问题是,我在表Products中只添加了类别id。我如何添加类别id和名称 结构我的数据库: 表品牌:idbrand,名称 表品牌:idbrand、namebrand、idmake、名称、价格、urlmake 我做了以下工作:C# 如何从一个Html.LabelFor向Category id和Name Category添加数据?,c#,asp.net,asp.net-mvc-3,entity-framework,C#,Asp.net,Asp.net Mvc 3,Entity Framework,在视图创建中,我想添加一个新产品。我需要从下拉列表中选择类别名称。问题是,我在表Products中只添加了类别id。我如何添加类别id和名称 结构我的数据库: 表品牌:idbrand,名称 表品牌:idbrand、namebrand、idmake、名称、价格、urlmake 我做了以下工作: // GET: /ShopManager/Create public ActionResult Create() { ViewBag.BrandID = new S
// 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>
//获取:/ShopManager/Create
公共操作结果创建()
{
ViewBag.BrandID=新选择列表(db.Brand,“BrandID”,“Name”);
返回视图();
}
//
//POST:/ShopManager/Create
[HttpPost]
公共行动结果创建(制作)
{
if(ModelState.IsValid)
{
db.Make.AddObject(Make);
db.SaveChanges();
返回操作(“索引”);
}
ViewBag.BrandID=新的选择列表(db.Brand,“BrandID”,“Name”,make.BrandID);
返回视图(make);
}
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
Марка телефона
@Html.LabelFor(model=>model.BrandID,“ББбб”)
@DropDownList(“BrandID”,String.Empty)
@Html.ValidationMessageFor(model=>model.BrandID)
@Html.LabelFor(model=>model.Name,“Маааааааааааааааа
@EditorFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@Html.LabelFor(model=>model.Price,“ааааааааааа
@EditorFor(model=>model.Price)
@Html.ValidationMessageFor(model=>model.Price)
@Html.LabelFor(model=>model.UrlMake,“ззжжжжж”)
@EditorFor(model=>model.UrlMake)
@Html.ValidationMessageFor(model=>model.UrlMake)
@ActionLink(“返回列表”、“索引”)
How me add-in Table Make BrandID和Brand Name of Brand(类别名称)?由于您有一个Brand Table,因此也不必在Make中存储Brand的名称,您可以通过一个简单的连接来获得它。无论如何,如果您确实想在创建中这样做,您可以将其设置为以下代码
make.NameBrand = db.Brand.Where(b => b.idbrand == make.idbrand).SingleOrDefault().namebrand;
我不理解。我如何添加make.NameBrand添加到表make-on-View-Create?