C# と SQL Server 2005 を使用して ASP .Net MVC 3 アプリケーションを開発しています。
Entity Framework と Code First Method も使用しています。
私の問題は、ベースに記録したいときに値が NULL を渡すことです。
エラーは次のとおりです。
列 'ID_Gamme' テーブル 'Flux.dbo.Gamme' に値 NULL を挿入できません。この列は NULL 値を受け入れません。挿入に失敗しました。ステートメントは終了されました
説明すると、3 つのテーブルがあります: Profile_Ga (ID_Gamme(PK),.....) Poste (ID_Poste(PK),....) Gamme (ID_Poste(PK),ID_Gamme(PK),.... ) は、Poste と Profile_Ga の間の関係テーブルです。
そして、他の2つのテーブルのパラメーターを使用して、テーブルGammeに記録したいと思います。
これは、パラメータを入力するフォームです。
それでは、ビューから始めましょう。これは Index のコードです。
<% using (Html.BeginForm("Create", "Anouar"))
{ %>
<div><%:Html.Label("Gamme :")%><%: Html.DropDownList("SelectedProfile_Ga", new SelectList(Model.Profile_GaItems, "ID_Gamme", "ID_Gamme"))%> <input type="button" value="Configurer" id="btnShowGestion" /></div>
<div id="divGestion"><%: Html.Partial("Gestion", Model) %></div>
<% } %>
and this is the Partial View 'Gestion.ascx' :
<fieldset class="parametrage">
<legend>Gestion de Gamme</legend>
<div><%:Html.Label("Poste :")%><%: Html.DropDownList("SelectedPoste", Model.PostesItems)%><input type="checkbox" name="option1" value="Poste Initial" id= "chkMain" onclick="test();"/>Poste Initial<input type="checkbox" name="option2" value="Poste Final" id= "chkFirst" onclick="test2();"/>Poste Final</div>
<div><%:Html.Label("Nombre de Passage :")%><%: Html.EditorFor(x=>x.YourGammeModel.Nbr_Passage)%></div>
<div><%:Html.Label("Position :")%><%: Html.EditorFor(x=>x.YourGammeModel.Position)%></div>
<div><%:Html.Label("Poste Précédent :")%><%: Html.DropDownList("PostePrecedentSelected", Model.PostesItems)%></div>
<div><%:Html.Label("Poste Suivant :")%><%: Html.DropDownList("PosteSuivantSelected", Model.PostesItems)%></div>
<div><input type="submit" value="Enregistrer" id="btnSave" /></div>
</fieldset>
これは、ビュー 'Index' に入力するコントローラーです。
public class ProfileGaController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /ProfileGa/
[HttpGet]
public ActionResult Index(Profile_Ga profile_ga, Poste poste)
{
var viewModel = new FlowViewModel();
viewModel.PostesItems = new SelectList(db.Postes.ToList(), "ID_Poste", "ID_Poste");
//viewModel.PostesItems = db.Postes.ToList() ?? new List<Poste>();
viewModel.Profile_GaItems = db.Profil_Gas.ToList();
viewModel.GaItems = db.Gammes.ToList();
return View(viewModel);
}
そして、これは PartialView を生成する Controller です:
public class AnouarController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /Anouar/
public ActionResult Gestion(FlowViewModel model)
{
model.YourGammeModel = new Gamme();
return PartialView(model);
}
[HttpPost]
public ActionResult Create(FlowViewModel model)
{
if (ModelState.IsValid)
{
db.Gammes.Add(model.YourGammeModel);
db.SaveChanges();
return RedirectToAction("Gestion");
}
return View(model.YourGammeModel);
}
}
注 : このコントローラーは、ベースに記録できるようにする「作成」機能を含むコントローラーです。
最後に、これは属性を含む ViewModel です。
public class FlowViewModel
{
[Key]
public string IDv { get; set; }
[NotMapped]
public SelectList PostesItems { get; set; }
public List<Profile_Ga> Profile_GaItems { get; set; }
public List<Gamme> GaItems { get; set; }
public Gamme YourGammeModel { get; set; }
public int SelectedProfile_Ga { get; set; }
public int SelectedGamme{ get; set; }
public int SelectedPoste { get; set; }
public int PostePrecedentSelected { get; set; }
public int PosteSuivantSelected { get; set; }
}
ただし、「YourGammeModel」にはすべてのプロパティが設定されていないと思います。それが問題です。
GammeContext のコード:
public class GammeContext : DbContext
{
public GammeContext()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<GammeContext>());
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Ns_AFaire> Ns_AFaires { get; set; }
public DbSet<Famille> Familles { get; set; }
public DbSet<Fonction> Fonctions { get; set; }
public DbSet<Fonction_Poste> Fonction_Postes { get; set; }
public DbSet<Gamme> Gammes { get; set; }
public DbSet<Historique> Historiques { get; set; }
public DbSet<Ligne> Lignes { get; set; }
public DbSet<Phase> Phases { get; set; }
public DbSet<Poste> Postes { get; set; }
public DbSet<Produit> Produits { get; set; }
public DbSet<Profile_Ga> Profil_Gas { get; set; }
public DbSet<Sous_Famille> Sous_Familles { get; set; }
public DbSet<UF> UFs { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Num_Serie> Num_Series { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
}
public DbSet<FlowViewModel> FlowViewModels { get; set; }
}