2016-05-15 21 views
-1

私はASP.NET MVC Webアプリケーションで作業していますので、別のテーブルから値を挿入したいのですが(ドロップダウンリストで取得します)、投稿しようとするとこれを取得:FOREIGN KEY制約 "FK_dbo.Products_dbo.Subcategories_SubcategoryId" と競合INSERTステートメントがFOREIGN KEY制約のドロップダウンリストと競合しました

INSERT文を。競合「 」は、データベース「ProyectName」、テーブル「dbo.Subcategories」、列 「SubcategoryId」で発生しました。ステートメントは終了されました。

製品モデル:

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を取得していないユニークです!

は、CREATE VIEWをMY 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> 
+0

あなたのアクションメソッドを作成し、model.SelectedSubCategory' 'の値が何であるかを確認し、それが有効なサブカテゴリーID – Shyju

+0

あるかどうかにブレークポイントを置きます'model.SelectedSubCategory'値を取得する' 0' @Shyju –

+0

使用すると、投稿する前にドロップダウンから項目を選択しましたか? – Shyju

答えて

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

これは理にかなっていません。問題をデバッグするのは、model.SelectedSubcategoryは常に0で、ユニークなサブカテゴリには0があります。 –

+0

したがって、基本的にフォームのデフォルトオプションは "-Selecciona una opcion- "は空の値を持つべきですMVCはその値をintに強制しようとします。つまり、intのデフォルト値は0になります。つまり、おそらく存在しない0を挿入しようとしますサブカテゴリ表はあなたが得ているエラーです。しかし、すべてのオプションについて0を取得している場合、私はより多くの情報が必要です。 HTMLはどのように表示されますか? (Html.BeginForm()){%> 'および' <% } %>' –

+0

はい、それは使用している、私は私の質問に完全なビューをアップロード –

関連する問題