INSERT语句与FOREIGN KEY约束下拉列表冲突

时间:2016-05-15 18:00:33

标签: c# sql-server asp.net-mvc

我在ASP.NET MVC Web应用程序中工作,所以我想从另一个表中插入值(我用dropdownlist获取),但是当我尝试发布时,我得到了这个:

  

INSERT语句与FOREIGN KEY约束冲突   " FK_dbo.Products_dbo.Subcategories_SubcategoryId&#34 ;.冲突   发生在数据库" ProyectName",table" dbo.Subcategories",列   ' SubcategoryId&#39 ;.声明已经终止。

产品型号:

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

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

}

子类别模型:

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

获取控制器

   public ActionResult Create()

    {
        var subcategoryList = new ProductViewModel
        { 
            Subcategory = new SelectList(db.SubcategoriesList, "SubcategoryId", "Name") 
        }; 

        return View(subcategoryList);
    }

后控制器(服务方法):

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

查看:

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

所以我没有发现我的代码有任何错误,这有什么问题?任何人都可以帮我解决这个问题吗?

我的问题是独一无二的,因为model.SelectedSubcategory不会从另一个表中获取Id值,它总是得到0所以在其他问题中没有与它进行任何比较

提前谢谢!

我现在创造视图

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

1 个答案:

答案 0 :(得分:0)

没有什么需要选择子类别。如果尚未对子类别进行选择,则可能会出现此错误。您应该使子类别成为必需,或者将null设为可以返回的值。

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

后控制器(服务方法):

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