我在引用在视图模型中使用实体框架的表时遇到问题。
这是Model,我不太确定为什么要将该类称为ViewModel,但是我没有给它命名。
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2}
characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation
password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
}
我认为引用此模型的视图模型非常简单:
public class UserCreateNewViewModel
{
public RegisterViewModel Register = new RegisterViewModel();
}
在我的控制器中,我有:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserCreateNew(RegisterViewModel model)
{
var m = new UserCreateNewViewModel();
if (ModelState.IsValid)
{
string registerToken =
WebMatrix.WebData.WebSecurity.CreateUserAndAccount(model.UserName,
model.Password, new { Email = model.Email }, true);
SimpleSecurity.WebSecurity.ConfirmAccount(registerToken);
string confirmationToken =
WebMatrix.WebData.WebSecurity.GeneratePasswordResetToken(model.UserName);
dynamic email = new Email("ChngPasswordEmail");
email.To = model.Email;
email.UserName = model.UserName;
email.baseUrl = GetBaseUrl();
email.ConfirmationToken = confirmationToken;
email.Send();
}
return RedirectToAction("UserList");
}
我觉得很奇怪,我试图将RegisterViewModel作为参数传递,然后尝试将其实例化为UserCreateViewModel(),但是我不确定如何通过UserCreateViewModel将registerViewModel放入视图。
查看:
@model ComtrexCloudReporting.Models.UserCreateNewViewModel
@{
ViewBag.Title = "UserCreate";
}
<div class="spacerBody">
<h2 class="admin-home-link orange-titles orange-titles-large">@Html.ActionLink("Create Users", "AdminIndex")</h2>
<div class="to-link navlinks"> @Html.ActionLink("Users", "UserList") | @Html.ActionLink("Manage User Role", "RoleManageUser")</div>
<p> </p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Registration Form</legend>
<ol class="comtrexBlue-text">
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
</li>
</ol>
<button class="btn larger" type="submit">Create</button>
</fieldset>
}````