传入字典的模型项的类型为'MVC_Web_App.Models.Products'

本文关键字:Web App Products Models MVC 字典 类型 模型 | 更新日期: 2023-09-27 18:14:50

我正在设计一个测试应用程序,控制器为

 [HttpGet]
    public ActionResult EditSingleProduct(string PrdNm)
    {
        try
        {
            List<Products> ProductGrid = new List<Products>();
            string UserAdded = Session["User"].ToString();
            SqlConnection Prodct = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
            Prodct.Open();
            using (SqlCommand command = new SqlCommand("", Prodct))
            {
                command.CommandText = "Select * from [Rush].[dbo].[Product] where ProductName='" + PrdNm + "'";
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                DataTable dtTable = dataSet.Tables[0];
                foreach (DataRow dr in dtTable.Rows)
                {
                    Products Prd1 = new Products();
                    Prd1.ProductName = dr["ProductName"].ToString();
                    Prd1.ProductType = dr["ProductType"].ToString();
                    Prd1.ProductPrice = dr["ProductPrice"].ToString();
                    Prd1.Quality1 = dr["Quality1"].ToString();
                    Prd1.Quality2 = dr["Quality2"].ToString();
                    Prd1.Quality3 = dr["Quality3"].ToString();
                    ProductGrid.Add(Prd1);
                }

            }
            return View(ProductGrid.ToList());
        }
        catch (Exception ex)
        {
            return View();
        }
    }

和View

    <title>EditSingleProduct</title>
    @using (Html.BeginForm())
{@Html.TextBoxFor(Prd1 => Prd1.ProductName, new { @class = "form-control text-center", placeholder = "Product Name" })                        @Html.ValidationMessageFor(Prd1 => Prd1.ProductName)
@Html.TextBoxFor(Prd1 => Prd1.ProductType, new {@class = "form-control text-center", placeholder = "Product Type" })                        @Html.ValidationMessageFor(Prd1 => Prd1.ProductType)
@Html.TextBoxFor(Prd1 => Prd1.ProductPrice, new {@class = "form-control text-center", placeholder = "Product Price" })                        @Html.ValidationMessageFor(Prd1 => Prd1.ProductPrice)
@Html.TextBoxFor(Prd1 => Prd1.Quality1, new { @class = "form-control text-center", placeholder = "Specification-I" })
@Html.ValidationMessageFor(Prd1 => Prd1.Quality1)
@Html.TextBoxFor(Prd1 => Prd1.Quality2, new { @class = "form-control text-center", placeholder = "Specification-II" })
@Html.ValidationMessageFor(Prd1 => Prd1.Quality2)
@Html.TextBoxFor(Prd1 => Prd1.Quality3, new {@class = "form-control text-center", placeholder = "Specification-III" })
@Html.ValidationMessageFor(Prd1 => Prd1.Quality3)
<input type="submit" id="Updatebtn" value="Update Product" class="btn" />

但是我不能得到数据sql工作良好,我得到一个项目作为请求在每个循环中,我只是想用该数据填充文本框,用户可以编辑文本框和更新数据和HTTP邮政编码工作良好

传入字典的模型项的类型为'MVC_Web_App.Models.Products'

这看起来像是你试图传递一个集合给你的视图,这是为单个对象设计的