在MVC中从多个部分视图将数据插入数据库 [英] Insert data into database from multiple partial views in MVC

查看:90
本文介绍了在MVC中从多个部分视图将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......

和所有其他人一样我也在这里讨论我的代码问题...

我是MVC的新手,我想插入来自多个局部视图的数据,但我遇到了问题。我已经从类别模型填充了下拉列表,我还有一个模型是产品模型。除dropdownlist之外的所有输入字段都是剃须刀,使用产品型号,但下拉列表是剃须刀,使用Category Model.Remember!我在这个项目中使用ADO.NET而不是EntityFramework。我在后端有一个关系数据库,它包含产品和类别关系。一切正常,但当我点击提交按钮时,我想在下拉列表中插入所选项目的ID,但我面临一个错误

这是...



Hi everyone...
just like all others i am also here to discuss my code problem...
I am new to MVC and i want to insert data from multiple partial views but i am facing a problem. I have populated the dropdownlist from "Category" model and i have one more model which is "Product" Model. all input fields except dropdownlist are razor by using Product Model but the dropdownlist is razor by using Category Model.Remember! I am using ADO.NET in this project not EntityFramework.I have a relational database at back end which contains the product and category relation. Everything works fine but when i click on submit button i want to insert the ID of the selected item in Dropdownlist but i face an error
which is...

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_Catagories". The conflict occurred in database "MVC-DB", table "dbo.Catagories", column 'Cat_ID'.





什么我试过了:



这里我正在尝试..

函数在数据库中插入数据



What I have tried:

Here what i am trying..
Function In Database to insert data

public void CreateProducts(Products product)
   {
       CategoryAdminPanel cat = new CategoryAdminPanel();
       string query = "Insert into Products (P_Name,P_Image,P_Price,P_CatagoryID) Values (@name,@image,@price,@category)";
       SqlConnection con = new SqlConnection(con_string);
       con.Open();
       SqlCommand cmd = new SqlCommand(query,con);
       cmd.Parameters.AddWithValue("@name",product.P_Name);
       cmd.Parameters.AddWithValue("@image", product.P_Image);
       cmd.Parameters.AddWithValue("@price",product.P_Price);
       cmd.Parameters.AddWithValue("@category", cat.Cat_ID);
       cmd.ExecuteNonQuery();
       con.Close();
   }





我的控制器..



My Controller..

[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult CreateProductsByAjaxCall(Products product)
       {
           if (ModelState.IsValid)
           {
               db.CreateProducts(product);
           }
           return RedirectToAction("Index");
       }





我的产品型号



My Product Model

public class Products
{
    public int P_ID { get; set; }
    public string P_Name { get; set; }
    public string P_Image { get; set; }
    public int P_Price { get; set; }
    public int P_CatedoryID { get; set; }
}





我的Catagory模型..





My Catagory Model..

public class CategoryAdminPanel
    {
        public int Cat_ID { get; set; }
        [Required]
        public string Cat_Name { get; set; }
    }

推荐答案

我找到了解决这个问题的方法..问题是当我将viewbage值传递给我的部分视图它工作正常,但当我选择ddl值并尝试提交到数据库时,它无法识别选择了哪个ddl值,所以我需要使用P_CategoryID作为DDL id,这是我的产品类属性而不是viewbag保留关键字类别



这是我的控制器填充DDL:



I have found the solution to this problem.. the problem is when i pass the viewbage value to my partial view it works fine but when i select the ddl value and trying to submit into database it does not recognize which value of ddl is selected so i need to use "P_CategoryID" as DDL id which is my "product" class property instead of viewbag reserve keyword "Category"

here is my controller that populate the DDL:

public ActionResult CreateProductsByAjaxCall()
     {
         ViewBag.P_CatedoryID = new SelectList(db.GetALLCategoriesForProducts(), "Cat_ID", "Cat_Name");
         //ViewBag.Categories = db.GetALLCategoriesForProducts().Select(x => new SelectListItem { Text = x.Cat_Name, Value = x.Cat_ID.ToString() });
         return PartialView();
     }





将下拉列表的ID从类别更改为P_CatedoryID



by changing the id of dropdown list from "Category" to "P_CatedoryID"

@Html.DropDownList("Category", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })





现在我的查看代码看起来像





Now my view code looks like

@Html.DropDownList("P_CatedoryID", (SelectList)ViewBag.RequiredKey, new { @class = "form-control" })


这篇关于在MVC中从多个部分视图将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆