如何通过在Dropdownlist中显示另一个表中的数据来发布数据库中的数据

时间:2019-05-02 13:23:10

标签: c# asp.net-mvc

我的视图有问题。我有一个产品模型,我想添加产品。在其中,有一个<名称>下拉列表,其类别名称来自另一个模型。我想选择类别,然后获取该类别的ID并将其放入我的产品模型中,因为它具有该类别的外键。基本上,我希望显示类别名称以及是否有人选择它。它应该发布该类别的ID,以便我可以将其放入我的产品模型中。

这是我的产品视图

@using Test2.Models;
@model ViewModel

@{
    ViewBag.Title = "Product";
    Layout = "~/Views/Shared/masternav1.cshtml";


}

<h2>Product</h2>

<link href="~/Scripts/StyleSheet1.css" rel="stylesheet" />

@using (@Html.BeginForm("Product", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="container">
        <form action="action_page.php">

            <div class="imgcontainer">
                <img src="~/Content/Images/product.jpg" alt="Avatar" class="avatar" />

            </div>

       @Html.Partial("_AdminProduct",Model.Productmodel)


            @Html.DropDownListFor(model => model.Productmodel.pro_fk_cat, new SelectList(new string[] { "Select Category", Model.CategoryModel.CategoryName }, "Select Category"), htmlAttributes: new { @class = "form-control" })


              <div>  

                <br />
                <input type="submit" value="Sign Up" class="btn btn-success btn-block" />
                <span style="color:red;">@ViewBag.error </span>

            </div>

            <div class="container" style="background-color:#f1f1f1">
                <button type="button" class="cancelbtn">Cancel</button>
            </div>

        </form>
    </div>
}



这是我的产品部分视图。

@model Test2.Models.Product

<div>
    <label for="uname"><b>Product Name</b></label>
    @*<input type="text" placeholder="Enter Username" name="uname" required="required">**@
    @Html.TextBoxFor(x => x.ProductName, new { @placeholder = "Product Name", @required = "required" })
    <br />
    <label for="psw"><b>Upload Image</b></label>
    @*<input type="password" placeholder="Enter Password" name="psw" required>*@
    <input type="file" name="Imgfile" id="Imgfile" class="form-control" required="required" />
    <br />
    <label for="psw"><b>Product Discription</b></label>
    @*<input type="password" placeholder="Enter Password" name="psw" required>*@
    @Html.TextAreaFor(x => x.ProductDes, new { @placeholder = "Product Discription", @required = "required", @class = "form-control" })
    <br />
    <label for="psw"><b>Product Price</b></label>
    @*<input type="password" placeholder="Enter Password" name="psw" required>*@
    @Html.TextBoxFor(x => x.Productprice, new { @placeholder = "Product Price", @required = "required" })
    <br />

    <label for="psw"><b>Product Category</b></label>
    @*@Html.DropDownListFor(x => x.pro_fk_cat, new SelectList(Model.),"Select Category")*@


</div>

这是我的控制人

 public ActionResult Product()
        {
            if (Session["ad_id"] == null)
            {
                return RedirectToAction("AdminLogin");
            }
           ViewModel mymodel = new ViewModel();
            mymodel.CategoryData = GetCategory();
            mymodel.Productmodel = GetProduct();
            return View(mymodel);
        }
        [HttpPost]
        public ActionResult Product(Product p,HttpPostedFileBase Imgfile)
        {
            string path = uploadingfile(Imgfile);
            if (path.Equals("-1"))
            {
                ViewBag.error = "Image Could not be Uploaded";
            }
            else
            {
                Product pro = new Product();


                pro.ProductName = p.ProductName;
                pro.ProductImage = path;
                pro.ProductDes = p.ProductDes;
                pro.Productprice = p.Productprice;
                pro.pro_fk_ad = Convert.ToInt32(Session["ad_id"].ToString());
                pro.pro_fk_cat = p.pro_fk_cat;
                db.Products.Add(pro);

                db.SaveChanges();
                return RedirectToAction("Category");
            }

            return View();
        }

这是我的产品型号


namespace Test2.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public string ProductImage { get; set; }
        public string ProductDes { get; set; }
        public int Productprice { get; set; }
        public Nullable<int> pro_fk_cat { get; set; }
        public Nullable<int> pro_fk_ad { get; set; }

        public virtual Admintb Admintb { get; set; }
        public virtual Category Category { get; set; }
    }
}

这是我的类别模型


namespace Test2.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Category
    {
        public Category()
        {
            this.Products = new HashSet<Product>();
        }

        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
        public string CategoryImg { get; set; }
        public Nullable<int> CategoryAdminID { get; set; }

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

这是我的ViewModel,用于在单个View中检索多个模型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test2.Models
{
    public class ViewModel
    {
        public IEnumerable<Admintb> AdminData { get; set; }
        public IEnumerable<Category> CategoryData { get; set; }

        public IEnumerable<Usertb> UserDt { get; set; }

        public Product Productmodel { get; set; }

        public Admintb Adminmodel { get; set; }

        public Usertb Usermodel { get; set; }

        public Category CategoryModel { get; set; }


        public Category Categryselection { get; set; }

    }


    public enum Category
    {

    }
} 

1 个答案:

答案 0 :(得分:-1)

您只需要在控制器中编写代码即可获取类别名称并返回ID。

类似这样的东西:

myProduct.category = categories.FirstOrDefault(c=> c.Name == categoryName).Id;

当您的categoryName是用户选择的类别时。

这真的很简单。

祝你好运!