私はこのアプリケーションを自分のアプリケーションで実行しています。 私はメインテーブルを作成するために必要な3つのテーブル、1つのメインテーブルと2つの他のテーブルを持って、メインテーブルは2つのテーブルに依存し、彼らは制約に接続されています。 私はメインテーブルを持っています:Word 次に、2つのテーブルがあります:難易度とカテゴリ。 難易度:簡単に作成し、カテゴリ:自然を作成しました。 これで2つの属性でできるだけ多くの単語を作成することができますが、それはエラーです。 私はこのようASP.Net MVC登録バグを作成する
Dificulty -> Category -> Word
または
Category -> Difficulty -> Word
を作成する場合、私は唯一の単語を作成することができます。 そのパスを作成せずに単語を作成することはできません。その理由はわかりません。 値は、2つのコンボボックスで呼び出されるデータベースに格納され、1つは難易度に、もう1つはカテゴリに格納されます。 単語を作成したい場合は、新しいカテゴリと新しい難易度を作成する必要があります。それ以外の場合はnullとして返します。
これは私のモデルビューです:
public partial class palavra
{
public int id_pal { get; set; }
public Nullable<int> LETRAS { get; set; }
public string PISTA { get; set; }
public int id_cat { get; set; }
public int id_dificuldade { get; set; }
public string nomepalavra { get; set; }
public virtual categoria categoria { get; set; }
public virtual dificuldade dificuldade { get; set; }
}
これは私のコントローラである:
public ActionResult Index()
{
var palavras = db.palavras.Include(p => p.categoria).Include(p => p.dificuldade);
return View(palavras.ToList());
}
// GET: palavras/Create
public ActionResult Create()
{
ViewBag.id_cat = new SelectList(db.categorias, "id_cat", "TIPO_CATEGORIA");
ViewBag.id_dificuldade = new SelectList(db.dificuldades, "id_dificuldade", "TIPO_DIFICULDADE");
return View();
}
// POST: palavras/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id_pal,LETRAS,PISTA,id_cat,id_dificuldade,nomepalavra")] palavra palavra)
{
if (ModelState.IsValid)
{
db.palavras.Add(palavra);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.id_cat = new SelectList(db.categorias, "id_cat", "TIPO_CATEGORIA", palavra.id_cat);
ViewBag.id_dificuldade = new SelectList(db.dificuldades, "id_dificuldade", "TIPO_DIFICULDADE", palavra.id_dificuldade);
return View(palavra);
}
これが私の見解です:
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.nomepalavra)
</th>
<th>
@Html.DisplayNameFor(model => model.LETRAS)
</th>
<th>
@Html.DisplayNameFor(model => model.PISTA)
</th>
<th>
@Html.DisplayNameFor(model => model.categoria.TIPO_CATEGORIA)
</th>
<th>
@Html.DisplayNameFor(model => model.dificuldade.TIPO_DIFICULDADE)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.nomepalavra)
</td>
<td>
@Html.DisplayFor(modelItem => item.LETRAS)
</td>
<td>
@Html.DisplayFor(modelItem => item.PISTA)
</td>
<td>
@Html.DisplayFor(modelItem => item.categoria.TIPO_CATEGORIA)
</td>
<td>
@Html.DisplayFor(modelItem => item.dificuldade.TIPO_DIFICULDADE)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id_pal }) |
@Html.ActionLink("Details", "Details", new { id=item.id_pal }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id_pal })
</td>
</tr>
}
私は私が手を挿入するために私のコードを実行するたびにこのエラー:
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code
Additional information: An error occurred while updating the entries. See the inner exception for details.
詳細を確認すると、「カテゴリ」テーブルと「難易度」テーブルがヌルですが、他のフィールドはすべて私が与えた情報であることがわかります。あなたが使用するエンティティフレームワークを必要としている場合
My Database Schema is the following:
Category connects to Word and Difficulty connects to Word.
Both Category and Difficulty are a 1 to many relationship with the table Word.
HttpPostのpalavraアクションメソッドにブレークポイントを設定し、必要なプロパティ値をすべて確認してください。 – Shyju