c# - 当我在 ASP MVC 、 SQL 和 Entity Framework 的基础中记录时,值传递 NULL

标签 c# sql-server visual-studio-2010 asp.net-mvc-3 entity-framework

我正在使用 C# 和 SQL Server 2005 开发 ASP .Net MVC 3 应用程序。

我也在使用 Entity Framework 和 Code First 方法。

我的问题是当我想在 base 中记录时,值传递 NULL。

enter image description here

错误是:

Can not insert the value NULL into column 'ID_Gamme' table 'Flux.dbo.Gamme'. This column does not accept NULL values. INSERT failed. The statement has been terminated

为了解释,我有 3 个表: Profile_Ga(ID_Gamme(PK),.....) 发布(ID_Poste(PK),....) Gamme(ID_Poste(PK),ID_Gamme(PK),....)是Poste和Profile_Ga的关系表。

我想使用其他 2 个表中的参数在表 Gamme 中记录。

这是我将在其中输入参数的表单: form

那么让我们从 View 开始,这是索引的代码:

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

这是填充 View “索引”的 Controller :

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

注意:这个 Controller 是一个包含允许我们在基地记录的“创建”功能的 Controller 。

最后这是包含属性的 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; }


    }

最佳答案

将模型返回给 Controller 时,您需要包含模型中的所有字段

使用 <%: Html.HiddenFor(x=>x.YourGammeModel.[[Field]]%>对于每个字段

关于c# - 当我在 ASP MVC 、 SQL 和 Entity Framework 的基础中记录时,值传递 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604976/

相关文章:

c++ - 错误 : Element <EnableEnhancedInstructionSet> has an invalid value of "NoExtensions"

c# - 通过 WCF 服务从接口(interface)引用方法

java - 为什么不直接为所有对象声明 Object 类呢?

sql - 如何使用 PIVOT 根据日期排列数据

java - 在 spring 中使用 SQLServerConnectionPoolDataSource

visual-studio-2010 - Visual Studio 在哪里存储文件属性?

c# - HttpWebRequest 是否自动处理证书验证?

c# - 如何部署 "SQL Server Express + EF"应用程序

sql-server - LEFT OUTER JOIN (SELECT * FROM TABLE) 这可能吗?

c++ - 在 MFC C++ 中使用多线程