C# 在筛选和保存数据时,如何从数据库列中获取passnull值?
我在数据库列中有几个字段,表名中有一个要保存的字段列表。有些人得到保存,而另一些人没有得到保存,它们显示为空(Dropdownlist用于世界各国、饮食要求和StreetaddressLine)。从数据库中查看下面的模式 如何传递空对象,这是我的ASP.NET MVC web表单。我需要一些帮助C# 在筛选和保存数据时,如何从数据库列中获取passnull值?,c#,sql-server,asp.net-mvc,entity-framework,C#,Sql Server,Asp.net Mvc,Entity Framework,我在数据库列中有几个字段,表名中有一个要保存的字段列表。有些人得到保存,而另一些人没有得到保存,它们显示为空(Dropdownlist用于世界各国、饮食要求和StreetaddressLine)。从数据库中查看下面的模式 如何传递空对象,这是我的ASP.NET MVC web表单。我需要一些帮助 // Model using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; name
// Model
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace eNtsaRegistrationTraining.Models
{
public class eNtsaRegForm
{
[Key]
public Guid Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public string Company { get; set; }
public string StreetAddress { get; set; }
public string StreetAddressLine { get; set; }
public string City { get; set; }
public string StateProvince { get; set; }
[Required]
public int ZipCode { get; set; }
public string Country { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string CellNumber { get; set; }
public string DietaryRequirements { get; set; }
}
这是我的控制器动作
// Controller
public ActionResult SubmitRegDetails(RegViewAndRoleViewModel eNtsaRegistration)
{
if(ModelState.IsValid)
{
eNtsaRegistration.RegForm.Id = Guid.NewGuid();
db.eNtsaRegForms.Add(eNtsaRegistration.RegForm);
db.SaveChanges();
return RedirectToAction("SaveRegForm");
}
return View(eNtsaRegistration);
}
这是我的数据层
// DAL(Data-Access-Layer)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using eNtsaRegistrationTraining.Models;
namespace eNtsaRegistrationTraining.DAL
{
public class eNtsaRegistration:DbContext
{
public eNtsaRegistration() : base("eNtsaRegistration")
{
} public DbSet<eNtsaRegForm> eNtsaRegForms { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<eNtsaRegistration>(null);
base.OnModelCreating(modelBuilder);
}
}
}
//DAL(数据访问层)
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用EntsRegistrationContraining.Models;
命名空间entsRegistrationTraining.DAL
{
公共类条目注册:DbContext
{
public eNtsaRegistration():base(“eNtsaRegistration”)
{
}公共DbSet eNtsaRegForms{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
Database.SetInitializer(null);
基于模型创建(modelBuilder);
}
}
}
不确定您使用的是什么框架,也不确定如何填充要保存的类实例的属性,但是
StreetAddress
,Country
和dietary requirements
字段在您给定的代码中未标记为required,因此NULL
是存储在数据库中的有效值
我会先改变这一点,然后看看您是如何填充这些字段的。您的问题是什么?ASP.NET MVC是一个web框架,而不是ORM或数据访问库。看起来您正在使用实体框架。哪个版本?事实上,问题是什么?如何将缺少的选项从视图传递到操作?如何存储丢失的选项?(这很简单,将相关属性设置为
null
或任何适当的“缺失”值)或者您首先要问的是为什么这些值为null?很可能是因为下拉/选择元素没有绑定到视图中的DTO。您尚未发布视图代码though@Panagiotis我使用的是实体框架,4版。是如何将缺少的选项从视图传递到操作。请给我举一些例子,然后可以很容易地理解。@ChrisDB我使用的是实体框架,在我的模型上,我使用的是数据-anotation@ChrisDB你能不能用伪代码告诉我,然后我会按照你说的做,我正在努力