我想将视图的数据从视图传递给带有ViewModel的Controller可能吗?我在发送数据时变为空。我的代码是:
我的ViewModel包含模型类引用。我想传递的数据是两个模型,即MasterSchoolInfo和MasterUserInfo,但是在控制器中提交已发布的值时显示的是公牛。任何有关这方面的帮助都会非常有帮助。我是Asp.Net MVC的新手。
如果我将模型传递给控制器,那么这工作正常,但是一旦我将其更改为viewmodel,它就开始向控制器发送null。
我认为从Model更改为ViewModel背后的想法是因为我想传递两个不同模型的数据并在控制器中使用它们。
视图模型
namespace ABC.ViewModels
{
public class UserInfoViewModel
{
public MasterSchoolInfo School { get; set; }
public MasterTeacherInfo Teacher{ get; set; }
public MasterStudentInfo Student { get; set; }
public MasterParentInfo Parent { get; set; }
public MasterUserInfo User { get; set; }
public MasterUserRole Role { get; set; }
}
}
控制器
[HttpPost]
public ContentResult CreateSchool(UserInfoViewModel _usrData)
{
var content = string.Empty;
if ((!String.IsNullOrEmpty(HttpContext.Session.GetString("UserEmail"))) && (!String.IsNullOrEmpty(HttpContext.Session.GetString("UserRole"))))
{
int UserId = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
string UserEmail = Convert.ToString(HttpContext.Session.GetString("UserEmail"));
string UserRole = Convert.ToString(HttpContext.Session.GetString("UserRole"));
byte[] salt = encryption.generatePasswordSalt("school");
string password = encryption.generateHashedPassword("school", salt);
if (UserRole == "Super Administrator")
{
_usrData.School.CreatedBy = UserEmail;
_usrData.School.CreatedOn = DateTime.Now;
_usrData.School.ApprovalStatus = true;
_usrData.School.Status = true;
MasterUserInfo userInfo = new MasterUserInfo();
userInfo.RoleId = 4;
userInfo.EmailId = _usrData.School.PrimaryEmailId;
userInfo.Salt = Convert.ToBase64String(salt).ToString();
userInfo.Password = password;
userInfo.CreatedBy = UserEmail;
userInfo.CreatedOn = DateTime.Now;
userInfo.ApprovalStatus = true;
userInfo.Status = true;
//string[] str = schoolInfo.PrimaryEmailId.Split('.');
//userInfo.Username = str[0].ToString();
userInfo.Username = _usrData.User.Username.ToString();
MasterSchoolInfo masterSchool = _context.Set<MasterSchoolInfo>().LastOrDefault();
if (masterSchool != null)
{
var lastschoolcode = masterSchool.OpinschoolCode;
var val = lastschoolcode.Substring(4, lastschoolcode.Length - 4);
int r = Convert.ToInt32(val) + 1;
string newusercode = "IESC000" + r;
userInfo.UserCode = newusercode;
_usrData.School.OpinschoolCode = newusercode;
}
else
{
string newusercode = "IESC000" + 1;
userInfo.UserCode = newusercode;
_usrData.School.OpinschoolCode = newusercode;
}
if (ModelState.IsValid)
{
_context.MasterUserInfo.Add(userInfo);
_context.SaveChanges();
MasterUserInfo masterUser = _context.Set<MasterUserInfo>().Last();
_usrData.School.UserId = masterUser.Id;
_context.MasterSchoolInfo.Add(_usrData.School);
_context.SaveChanges();
TempData["Message"] = "School Added Successfully!";
content = "Success";
}
else
{
content = "Error";
}
}
else
{
content = "Error";
}
}
else
{
content = "Error";
}
return Content(content);
}
答案 0 :(得分:1)
例如,如果您的代码是:
public class MasterSchoolInfo
{
public string name{get;set;}
}
你应该在视图中实现输入:
<input type="text" name="school.name">