私は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>
あなたのアクションメソッドを作成し、model.SelectedSubCategory' 'の値が何であるかを確認し、それが有効なサブカテゴリーID – Shyju
あるかどうかにブレークポイントを置きます'model.SelectedSubCategory'値を取得する' 0' @Shyju –
使用すると、投稿する前にドロップダウンから項目を選択しましたか? – Shyju