INSERT语句冲突与外键约束的DropDownList [英] The INSERT statement conflicted with the FOREIGN KEY constraint dropdownlist
问题描述
我在ASP.NET MVC Web应用程序工作,所以我想从另一个表中插入值(我得到它的DropDownList),但是当我尝试后,我得到这样的:
INSERT语句冲突与外键约束
FK_dbo.Products_dbo.Subcategories_SubcategoryId。冲突
发生于数据库ProyectName,表dbo.Subcategories列
SubcategoryId。该语句已终止。
块引用>产品型号:
公共类产品
{
公众诠释产品编号{搞定;组; }
公众诠释SubcategoryId {搞定;组; }
公共虚拟子目录子目录{搞定;组; }
公共字符串名称{;组; }
公共字符串$ P $ {psentation获得;组; }
公共字符串图像{搞定;组; }
公共字符串Alt键{获得;组; }
公共BOOL {请将isDeleted获得;组; }产品视图模型
公共类ProductViewModel
{ 公共字符串名称{;组; }
公共字符串$ P $ {psentation获得;组; }
公共字符串图像{搞定;组; }
公共字符串Alt键{获得;组; }
公众诠释SelectedSubcategory {搞定;组; }
公共IEnumerable的< SelectListItem> {子类别获得;组; }}子目录型号:
公共类子目录
{
公众诠释SubcategoryId {搞定;组; }
公众诠释的CategoryId {搞定;组; }
公共虚拟分类分类{搞定;组; }
公共字符串名称{;组; }
公共字符串描述{搞定;组; }
公共字符串图像{搞定;组; }
公共字符串Alt键{获得;组; }
公共字符串PDF {搞定;组; }
公共BOOL {请将isDeleted获得;组; }
公共BOOL IsInstalled的{搞定;组; }
}获取控制器
公众的ActionResult的Create() {
VAR subcategoryList =新ProductViewModel
{
子类别=新的SelectList(db.SubcategoriesList,SubcategoryId,姓名)
}; 返回查看(subcategoryList);
}邮政控制器(服务方法):
公共类ProductService:IProductService
{
私人EfDatabase DB =新EfDatabase(); 公共异步任务<串GT; CreateProduct(ProductViewModel模型)
{
变种产品=新产品
{
名称= model.Name,
presentation =模式。presentation,
图片= model.Image,
ALT = model.Alt,
SubcategoryId = model.SelectedSubcategory,
=请将isDeleted假
};
db.ProductsList.Add(产品);
等待db.SaveChangesAsync();
返回产品+ model.Name +已创建;查看:
@model Proyect.Models.ViewModels.ProductViewModel@ {
ViewBag.Title =创建;
} < H2>创建< / H> @using(Html.BeginForm())
{
@ Html.AntiForgeryToken()< DIV CLASS =形横>
< H4>产品与LT; / H4>
<小时/>
@ Html.ValidationSummary(真,新{@class =TEXT-危险})
< DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.SubcategoryId,SubcategoryId,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.DropDownListFor(M = GT; m.SelectedSubcategory,Model.Subcategory,-Selecciona UNA opcion-,新{@class =表格控})
@ Html.ValidationMessageFor(M = GT; m.SelectedSubcategory)
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Name,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=> model.Name,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=> model.Name,新{@class =TEXT-危险})
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号= GT;模型$ P $ psentation,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=方式>模式presentation,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=方式>模式presentation,新{@class =TEXT-危险})
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Image,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=> model.Image,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=> model.Image,新{@class =TEXT-危险})
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Alt,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=> model.Alt,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=> model.Alt,新{@class =TEXT-危险})
< / DIV>
< / DIV>
< DIV CLASS =表单组>
< DIV CLASS =COL-MD-偏移2 COL-MD-10>
<输入类型=提交值=创建类=BTN BTN-默认的/>
< / DIV>
< / DIV>
< / DIV>
}< DIV>
@ Html.ActionLink(返回目录,索引)
< / DIV>因此,我不发现我的code任何错误,有什么不好呢?任何一个可以帮助我解决这个问题?
我的问题是独一无二的,因为model.SelectedSubcategory不从另一个表获得Id值,它总是0,因此在其他问题没有做出与任何比较
三江源提前!
我现在创建视图
@model myPROYECT.Models.ViewModels.ProductViewModel@ {
ViewBag.Title =创建;
}< H2>创建< / H> @using(Html.BeginForm())
{
@ Html.AntiForgeryToken()
< DIV CLASS =形横>
< H4>产品与LT; / H4>
<小时/>
@ Html.ValidationSummary(真,新{@class =TEXT-危险})
< DIV CLASS =表单组>
< DIV CLASS =COL-MD-10>
@ Html.DropDownListFor(M = GT; m.SelectedSubcategory,Model.Subcategory,-Selecciona UNA opcion-,新{@class =表格控})
@ Html.ValidationMessageFor(M = GT; m.SelectedSubcategory)
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Name,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=> model.Name,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=> model.Name,新{@class =TEXT-危险})
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号= GT;模型$ P $ psentation,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=方式>模式presentation,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=方式>模式presentation,新{@class =TEXT-危险})
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Image,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=> model.Image,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=> model.Image,新{@class =TEXT-危险})
< / DIV>
< / DIV> < DIV CLASS =表单组>
@ Html.LabelFor(型号=> model.Alt,htmlAttributes:新{@class =控制标签COL-MD-2})
< DIV CLASS =COL-MD-10>
@ Html.EditorFor(型号=> model.Alt,新{htmlAttributes = {新@class =表格控}})
@ Html.ValidationMessageFor(型号=> model.Alt,新{@class =TEXT-危险})
< / DIV>
< / DIV>
< DIV CLASS =表单组>
< DIV CLASS =COL-MD-偏移2 COL-MD-10>
<输入类型=提交值=创建类=BTN BTN-默认的/>
< / DIV>
< / DIV>
< / DIV>
}< DIV>
@ Html.ActionLink(返回目录,索引)
< / DIV>
解决方案有什么要求要选择一个子类别。你很可能会进行选择后,没有提出子类别得到这个错误。你应该让需要的子类别或作出空的值可能回去。
公共类产品
{
公众诠释产品编号{搞定;组; }
公众诠释? SubcategoryId {搞定;组; }
公共虚拟子目录子目录{搞定;组; }
公共字符串名称{;组; }
公共字符串$ P $ {psentation获得;组; }
公共字符串图像{搞定;组; }
公共字符串Alt键{获得;组; }
公共BOOL {请将isDeleted获得;组; }
}
使用System.ComponentModel.DataAnnotations;公共类ProductViewModel
{
公众诠释? SubcategoryId {搞定;组; }
公共字符串名称{;组; }
公共字符串$ P $ {psentation获得;组; }
公共字符串图像{搞定;组; }
公共字符串Alt键{获得;组; }
[需要]
公众诠释? SelectedSubcategory {搞定;组; }
公共IEnumerable的子类别{搞定;组; }
}邮政控制器(服务方法):
公共类ProductService:IProductService
{
私人EfDatabase DB =新EfDatabase(); 公共异步任务<串GT; CreateProduct(ProductViewModel模型)
{
如果(!ModelState.IsValid)
返回视图(创建); 变种产品=新产品
{
名称= model.Name,
presentation =模式。presentation,
图片= model.Image,
ALT = model.Alt,
SubcategoryId = model.SelectedSubcategory,
=请将isDeleted假
};
db.ProductsList.Add(产品);
等待db.SaveChangesAsync();
返回产品+ model.Name +已创建;
}
}I'm working in ASP.NET MVC Web Application, so I want to insert values from another table (I get it with dropdownlist), but when I try to post, I get this:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Products_dbo.Subcategories_SubcategoryId". The conflict occurred in database "ProyectName", table "dbo.Subcategories", column 'SubcategoryId'. The statement has been terminated.
Products model:
public class Product { public int ProductId { get; set; } public int SubcategoryId { get; set; } public virtual Subcategory Subcategory { get; set; } public string Name { get; set; } public string Presentation { get; set; } public string Image { get; set; } public string Alt { get; set; } public bool IsDeleted { get; set; }
Product ViewModel
public class ProductViewModel { public string Name { get; set; } public string Presentation { get; set; } public string Image { get; set; } public string Alt { get; set; } public int SelectedSubcategory { get; set; } public IEnumerable <SelectListItem> Subcategory { get; set; } }
Subcategory Model:
public class Subcategory { public int SubcategoryId { get; set; } public int CategoryId { get; set; } public virtual Category Category { get; set; } public string Name { get; set; } public string Description { get; set; } public string Image { get; set; } public string Alt { get; set; } public string Pdf { get; set; } public bool IsDeleted { get; set; } public bool IsInstalled { get; set; } }
Get Controller
public ActionResult Create() { var subcategoryList = new ProductViewModel { Subcategory = new SelectList(db.SubcategoriesList, "SubcategoryId", "Name") }; return View(subcategoryList); }
Post Controller(Service method):
public class ProductService : IProductService { private EfDatabase db = new EfDatabase(); public async Task<string> CreateProduct(ProductViewModel model) { var product = new Product { Name = model.Name, Presentation = model.Presentation, Image = model.Image, Alt = model.Alt, SubcategoryId = model.SelectedSubcategory, IsDeleted = false }; db.ProductsList.Add(product); await db.SaveChangesAsync(); return "Product " + model.Name + "has been created";
View:
@model Proyect.Models.ViewModels.ProductViewModel @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Product</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.SubcategoryId, "SubcategoryId", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownListFor(m => m.SelectedSubcategory, Model.Subcategory, "-Selecciona una opcion-", new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.SelectedSubcategory) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Presentation, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Presentation, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Presentation, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Alt, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Alt, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Alt, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div>
So I don't found any error on my code, what's wrong with that? can any one help me to solve this problem?
My question is unique because model.SelectedSubcategory don't get Id value from another table, It always get 0 so in the other questions no make any comparision with that
Thankyou in advance!
MY CREATE VIEW NOW
@model myPROYECT.Models.ViewModels.ProductViewModel @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Product</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-10"> @Html.DropDownListFor(m => m.SelectedSubcategory, Model.Subcategory, "-Selecciona una opcion-", new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.SelectedSubcategory) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Presentation, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Presentation, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Presentation, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Alt, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Alt, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Alt, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div>
解决方案There's nothing that requires a Subcategory to be selected. You're likely to get this error when a selection hasn't been made for subcategory. You should make subcategory required or make null a value that's possible to get back.
public class Product { public int ProductId { get; set; } public int? SubcategoryId { get; set; } public virtual Subcategory Subcategory { get; set; } public string Name { get; set; } public string Presentation { get; set; } public string Image { get; set; } public string Alt { get; set; } public bool IsDeleted { get; set; } } using System.ComponentModel.DataAnnotations; public class ProductViewModel { public int? SubcategoryId { get; set; } public string Name { get; set; } public string Presentation { get; set; } public string Image { get; set; } public string Alt { get; set; } [Required] public int? SelectedSubcategory { get; set; } public IEnumerable Subcategory { get; set; } }
Post Controller(Service method):
public class ProductService : IProductService { private EfDatabase db = new EfDatabase(); public async Task<string> CreateProduct(ProductViewModel model) { if (!ModelState.IsValid) return View("Create"); var product = new Product { Name = model.Name, Presentation = model.Presentation, Image = model.Image, Alt = model.Alt, SubcategoryId = model.SelectedSubcategory, IsDeleted = false }; db.ProductsList.Add(product); await db.SaveChangesAsync(); return "Product " + model.Name + "has been created"; } }
这篇关于INSERT语句冲突与外键约束的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!