简体   繁体   English

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

[英]How can I post data in database by showing data from another table in Dropdownlist

I am having a problem with my View. 我的视图有问题。 I have a Product Model and I want to add products. 我有一个产品模型,我想添加产品。 In it, there is a dropdownlist of category Name which is coming from another model. 在其中,有一个类别名称的下拉列表 ,该类别来自另一个模型。 I want to select the category and then get the ID of that Category and put it in my Product Model Because it had a foreign key of that category. 我想选择类别,然后获取该类别的ID并将其放入我的产品模型中,因为它具有该类别的外键。 Basically, I want the category name to be shown as well as if someone selects it .it should post the Id of that Category so that I can put it in my product model. 基本上,我希望显示类别名称以及是否有人选择它。它应该发布该类别的ID,以便我可以将其放入我的产品模型中。

This is My Product View 这是我的产品视图

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



This is my Product Partial VIew. 这是我的产品局部视图。

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

This is my Controller 这是我的控制器

 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();
        }

This is my product model 这是我的产品型号


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; }
    }
}

This is my Category Model 这是我的类别模型


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; }
    }
}

This is my ViewModel which I am using to retrieve multiple models in a single View. 这是我的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
    {

    }
} 

You just need to write a code in your controller that get the category name and return the Id. 您只需要在控制器中编写代码即可获取类别名称并返回ID。

Something like this: 像这样:

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

when your categoryName is the category what the user chosen. 当您的categoryName是用户选择的类别时。

It's realy simple. 真的很简单。

good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择从另一个表的条目的下拉列表中存储数据,还是手动输入数据? - How do I choose to either store data from a dropdownlist of entries from another table, or manually input the data? 下拉列表显示空表的数据 - Dropdownlist showing data for empty table 如何将数据从下拉列表传递到数据库 - how to Pass data from dropdownlist to database 如何从数据库中提取数据并显示在下拉列表中 - How to pull data from database and display in dropdownlist 如何从数据库创建 DropDownList? - How can I make a DropDownList from the database? 我可以从一个 SQL 数据库中获取数据,然后使用 MVC C# 将该数据发布到不同服务器上的另一个 SQL 数据库中吗? - Can I get data from one SQL database and then post that data on another SQL database on a diferent server using MVC C# 如何将数据从数据表直接添加到数据库表? - How can I add data from DataTable to database table directly? 如何使用数据库中的值填充数据表 - how can I populate data table with values from a database 数据未显示在数据库的DropDownList中 - Data not shown in DropDownList from database 如何从数据库中的表读取数据并将其插入另一个数据库? - how to read data from a table in database and insert into another database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM