Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In the view Create i want to add a new product. I need from the drop down list to select the category name. The problem is, that i have in the table Products add only the category id. And how me add and name too of category?

Structure my DB:

Table Brand: idbrand, name.

Table Make: idbrand, namebrand, idmake, name, price, urlmake.

I do the following:
C#
//
        // 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);
        }


HTML
@model ShopRusWizards.Models.Make

@{
    ViewBag.Title = "Create";
}

<h2>Добавление</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@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>


How me add in Table Make BrandID and Name of Brand(name of Category)? Please help.

P.S. Sorry for my bad English
Posted
Updated 29-Feb-12 21:26pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900