如何根据另一个下拉列表MVC 4中的另一个选定值检索要在下拉列表中显示的数据

时间:2014-07-01 09:08:12

标签: asp.net-mvc entity-framework asp.net-mvc-4

我在我的数据库中首先使用实体​​框架代码构建mvc 4应用程序我有三个表Products和Category SubCategory其中Products表包含有关产品的信息,Category包含类别男人 - 女人 - 孩子和SubCategory包含SubCategory喜欢鞋子 - 连衣裙 - 适合每个子类别属于一个类别或更多类似子类别套装仅属于鞋类所属的人类别所有类别男人 - 女人 - 孩子

我用来构建这个数据库的类是

1.Products Classe:

[Bind(Exclude = "ProductID,Discount")]
public class Product
{
    [ScaffoldColumn(false)]
    public int ProductID { get; set; }

    public string ProductImageURL { get; set; }

    public string ProductName { get; set; }

    public decimal Price { get; set; }

    public int Quantity { get; set; }

    public int Sales { get; set; }

    public decimal Discount { get; set; }

    public int CategoryID { get; set; }
    public Category Category { get; set; }

    public string SubCategory { get; set; }

    public int ColorID { get; set; }
    public Color Color { get; set; }

    public int SizeID { get; set; }
    public Size Size { get; set; }

    public int CompanyID { get; set; }
    public Company Company { get; set; }

}

2.Category Class:

[Bind(Exclude = "CategoryID")]
public class Category
{

    [ScaffoldColumn(false)]
    public int CategoryID { get; set; }

    public string CategoryName { get; set; }

    public virtual ICollection<Product> Products { get; set; }

    public virtual ICollection<SubCategory> SubCategorys { get; set; }
}

3.Sub-Category Class:

[Bind(Exclude = "SubCategoryID")]
public class SubCategory
{

    [ScaffoldColumn(false)]
    public int SubCategoryID { get; set; }

    public string SubCategoryName { get; set; }

    public int CategoryID { get; set; }
    public Category Category { get; set; }
}

所以创建产品我使用这个逻辑在操作方法create:

中的产品控制器中执行此操作
    // GET: /Products/Create

    public ActionResult Create()
    {
        ViewBag.CategoryID = new SelectList(db.Categorys, "CategoryID", "CategoryName");
        ViewBag.SubCategoryID = new SelectList(db.SubCategorys, "SubCategoryID", "SubCategoryName");
        ViewBag.ColorID = new SelectList(db.Colors, "ColorID", "ColorName");
        ViewBag.SizeID = new SelectList(db.Sizes, "SizeID", "SizeName");
        ViewBag.CompanyID = new SelectList(db.Companyies, "CompanyID", "CompanyName");
        return View();
    }

 // POST: /Products/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.CategoryID = new SelectList(db.Categorys, "CategoryID", "CategoryName", product.CategoryID);
        ViewBag.SubCategoryID = new SelectList(db.SubCategorys, "SubCategoryID", "SubCategoryName");
        ViewBag.ColorID = new SelectList(db.Colors, "ColorID", "ColorName", product.ColorID);
        ViewBag.SizeID = new SelectList(db.Sizes, "SizeID", "SizeName", product.SizeID);
        ViewBag.CompanyID = new SelectList(db.Companyies, "CompanyID", "CompanyName", product.CompanyID);
        return View(product);
    }

并在创建视图中:

@model StopeAndShop.Models.Product

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<legend>Product</legend>
<div class="container">
    <div class="editor-label">
        @Html.LabelFor(model => model.ProductImageURL)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ProductImageURL)
        @Html.ValidationMessageFor(model => model.ProductImageURL)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ProductName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ProductName)
        @Html.ValidationMessageFor(model => model.ProductName)
    </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.Quantity)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Quantity)
        @Html.ValidationMessageFor(model => model.Quantity)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Sales)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Sales)
        @Html.ValidationMessageFor(model => model.Sales)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Discount)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Discount)
        @Html.ValidationMessageFor(model => model.Discount)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.CategoryID)
    </div>
    <div class="editor-field">
        @Html.DropDownList("CategoryID", string.Empty)
        @Html.ValidationMessageFor(model => model.CategoryID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SubCategory)
    </div>
    <div class="editor-field">
        @Html.DropDownList("SubCategoryID", string.Empty)
        @Html.ValidationMessageFor(model => model.SubCategory)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ColorID)
    </div>
    <div class="editor-field">
        @Html.DropDownList("ColorID", string.Empty)
        @Html.ValidationMessageFor(model => model.ColorID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SizeID)
    </div>
    <div class="editor-field">
        @Html.DropDownList("SizeID", string.Empty)
        @Html.ValidationMessageFor(model => model.SizeID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.CompanyID)
    </div>
    <div class="editor-field">
        @Html.DropDownList("CompanyID", string.Empty)
        @Html.ValidationMessageFor(model => model.CompanyID)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</div>
}

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

  @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    }

所以我的问题是如何仅检索所选类别的对应SubCategory。 因为在我的逻辑中,它检索下拉列表中的所有SubCategory,忽略所选的类别,我是mvc中的初学者,所以这是正确的方法来实现这一点通过查看包带来数据发送到视图或有其他正确的方式或首选
它看起来像那样:

并提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在您的子类别表中提供类别ID。 让类别表为:

CatId CatName
  1    Men
  2    Women

子类别表:

SubCatId SubCatName CatId
  1        Suit      1
  2       dresses    2

您将在下拉列表中获得相关数据。