我已设置为从数据库中读取数据,并使用MVC C#将数据显示给UI。但是,当我尝试调试时,尽管email列在该表上具有值,但User.Emails.Email仍显示Null值。我正确的方法吗?
这是我为此创建的控制器。
//控制器 var session = Session [“ currentusers”];
ConnectionDB data = new ConnectionDB();
data.connection();
var user = new UserLoginModel();
try
{
using (data.con)
{
SqlCommand users = new SqlCommand("uspDisplayUser", data.con);
users.Parameters.AddWithValue("@Email", session);
users.CommandType = CommandType.StoredProcedure;
data.con.Open();
using (var reader = users.ExecuteReader())
{
while (reader.Read())
{
if (!reader.IsDBNull(reader.GetOrdinal("UserID")))
{
user.UserID = reader.GetString(reader.GetOrdinal("UserID"));
}
if (!reader.IsDBNull(reader.GetOrdinal("FirstName")))
{
user.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
}
if (!reader.IsDBNull(reader.GetOrdinal("LastName")))
{
user.LastName = reader.GetString(reader.GetOrdinal("LastName"));
}
if (!reader.IsDBNull(reader.GetOrdinal("Email")))
{
user.Emails.Email = reader.GetString(reader.GetOrdinal("Email"));
}
}
}
data.con.Close();
}
return View(user);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
return View();
}
//模型UserModel 公共类UserModel { 公用字符串UserID {组; }
[Display(Name = "First Name*")]
[Required(ErrorMessage = "First Name is required.")]
public string FirstName { get; set; }
[Display(Name = "Last Name*")]
[Required(ErrorMessage = "Last Name is required.")]
public string LastName { get; set; }
public string ICNumber { get; set; }
public string Passport { get; set; }
public int Gender { get; set; } //Add Gender 15 May 2019 -Aidil-
public StatesModel States { get; set; }
public NationalityModel Nationalities { get; set; }
public UserLoginModel Emails { get; set; }
public CountryModel Countries { get; set; }
}
//模型UserLoginModel
public class UserLoginModel
{
[Required(ErrorMessage = "Email is required")]
public string Email { get; set; }
[Display(Name = "Password*")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required.")]
public string Password { get; set; }
[NotMapped]
[Required(ErrorMessage = "Confirm Password is required")]
[DataType(DataType.Password)]
[Compare("Password")]
public string ConfirmPassword { get; set; }
}
user.Emails.Email的电子邮件列将不为空