在ASP.NET MVC中提交表单后创建新的应用程序用户



我已经浏览了好几次代码,仍然不知道我可能做错了什么。在我的ASP.NET MVC应用程序中,我希望能够在通过视图模型提交表单后创建新用户。通过视图模型提交的部分信息将用于填充ApplicationUser模型。

这是我的视图模型

[Column(TypeName = "decimal(18,2)")]
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "LoanAmount", ResourceType = typeof(Resources))]
public decimal LoanAmount { get; set; }
[Display(Name = "LoanPurpose", ResourceType = typeof(Resources))]
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
public LoanPurpose LoanPurpose { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "FirstName", ResourceType = typeof(Resources))]
public string FirstName { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "LastName", ResourceType = typeof(Resources))]
public string LastName { get; set; }
[Display(Name = "DateOfBirth", ResourceType = typeof(Resources))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
public DateTime? DateOfBirth { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "StreetAddress", ResourceType = typeof(Resources))]
public string Street { get; set; }
[MaxLength(15)]
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "City", ResourceType = typeof(Resources))]
public string City { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "State", ResourceType = typeof(Resources))]
public string State { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "ZIP", ResourceType = typeof(Resources))]
public string ZipCode { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "Country", ResourceType = typeof(Resources))]
public string Country { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "Phone", ResourceType = typeof(Resources))]
public string Phone { get; set; }
[Column(TypeName = "decimal(18,2)")]
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "MonthlyHousingPayment", ResourceType = typeof(Resources))]
public decimal MonthlyHousingPayment { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[Display(Name = "EmploymentStatus", ResourceType = typeof(Resources))]
public EmploymentStatus EmploymentStatus { get; set; }
[Column(TypeName = "decimal(18,2)")]
[Display(Name = "IndividualYearlyIncome", ResourceType = typeof(Resources))]
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
public decimal IndividualYearlyIncome { get; set; }
[Column(TypeName = "decimal(18,2)")]
[Display(Name = "AdditionalYearlyIncome", ResourceType = typeof(Resources))]
public decimal? AdditionalYearlyIncome { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[EmailAddress(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "EmailInvalid")]
[Display(Name = "Email", ResourceType = typeof(Resources))]
public string Email { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[DataType(DataType.Password)]
[StringLength(100, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "StringLength", MinimumLength = 4)]
[Display(Name = "Password", ResourceType = typeof(Resources))]
public string Password { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
[StringLength(20, ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "StringLength", MinimumLength = 4)]
[Display(Name = "Username", ResourceType = typeof(Resources))]
public string UserName { get; set; }
[Required(ErrorMessageResourceType = typeof(ValidationResources), ErrorMessageResourceName = "PropertyValueRequired")]
public bool AcceptCreditReportTerms { get; set; }
public Guid ApplicationUserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }

这是我的控制器(CheckRateController(

// POST: CheckRate/Index
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(CheckYourRateViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
FirstName = model.FirstName,
LastName = model.LastName,
UserName = model.UserName,
PhoneNumber = model.Phone,
DateOfBirth = model.DateOfBirth,
State = model.State,
Street = model.Street,
City = model.City,
Country = model.Country
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// Add registered users to Customer role
await _userManager.AddToRoleAsync(user.Id, "Customer");
}
var loanApplication = new Loan
{
LoanPurpose = model.LoanPurpose,
LoanAmount = model.LoanAmount,
EmploymentStatus = model.EmploymentStatus,
MonthlyHousingPayment = model.MonthlyHousingPayment,
IndividualYearlyIncome = model.IndividualYearlyIncome,
AdditionalYearlyIncome = model.AdditionalYearlyIncome,
ApplicationUserId = Guid.Parse(user.Id)
};
db.Loans.Add(loanApplication);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View(model);
}

这就是我的视图如何开始(Index.cshtml(

<section class="main-chkout">
<div class="container">
@using (Html.BeginForm("Index", "CheckRate", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<h1>@Resources.CheckYourRate</h1>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="frm-bx">
<h2>How much would you like to borrow?</h2>
<div class="frm-gap">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.LoanAmount)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty lgap">
@Html.TextBoxFor(m => m.LoanAmount, new { placeholder = @Resources.LoanAmount })
<span>$</span>
</div>
@Html.ValidationMessageFor(model => model.LoanAmount, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.LoanPurpose)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.EnumDropDownListFor(model => model.LoanPurpose)
<i class="fa fa-caret-down" aria-hidden="true"></i>
</div>
@Html.ValidationMessageFor(model => model.LoanPurpose, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div> <!-- Form tab End -->
<div class="frm-bx">
<h2>Tell Us About Yourself</h2>
<div class="frm-gap">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
<label for="">Full Name</label>
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="row">
<div class="col-md-6">
<div class="inp-sty">
@Html.TextBoxFor(m => m.FirstName, new { placeholder = @Resources.FirstName })
</div>
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
<div class="col-md-6">
<div class="inp-sty">
@Html.TextBoxFor(m => m.LastName, new { placeholder = @Resources.LastName })
</div>
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.DateOfBirth)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.TextBoxFor(m => m.DateOfBirth, new { placeholder = "Date of Birth (MM/DD/YYYY)" })
</div>
@Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.Street)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.TextBoxFor(m => m.Street, new { placeholder = @Resources.StreetAddress })
</div>
@Html.ValidationMessageFor(model => model.Street, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
<label for="">City & State</label>
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="row">
<div class="col-md-6">
<div class="inp-sty">
@Html.TextBoxFor(m => m.City, new { placeholder = @Resources.City })
</div>
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
<div class="col-md-6">
<div class="inp-sty">
<div class="inp-sty">
@Html.TextBoxFor(m => m.State, new { placeholder = @Resources.State })
</div>
@Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
<label for="">Zip & Phone</label>
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="row">
<div class="col-md-6">
<div class="inp-sty">
@Html.TextBoxFor(m => m.ZipCode, new { placeholder = @Resources.ZIP })
</div>
@Html.ValidationMessageFor(model => model.ZipCode, "", new { @class = "text-danger" })
</div>
<div class="col-md-6">
<div class="inp-sty">
@Html.TextBoxFor(m => m.Phone, new { placeholder = @Resources.PhoneNumber })
</div>
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div> <!-- Row End -->
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.MonthlyHousingPayment)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.TextBoxFor(m => m.MonthlyHousingPayment, new { placeholder = @Resources.MonthlyHousingPayment })
</div>
@Html.ValidationMessageFor(model => model.MonthlyHousingPayment, "", new { @class = "text-danger" })
</div>
</div> <!-- Row End Here -->
</div>
</div> <!-- Form tab End -->
<div class="frm-bx">
<h2>What is your income?</h2>
<div class="frm-gap">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.EmploymentStatus)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.EnumDropDownListFor(model => model.EmploymentStatus)
<i class="fa fa-caret-down" aria-hidden="true"></i>
</div>
@Html.ValidationMessageFor(model => model.EmploymentStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.IndividualYearlyIncome)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty lgap">
@Html.TextBoxFor(m => m.IndividualYearlyIncome, new { placeholder = @Resources.IndividualYearlyIncome })
<span>$</span>
</div>
@Html.ValidationMessageFor(model => model.IndividualYearlyIncome, "", new { @class = "text-danger" })
</div>
</div> <!-- Row End Here -->
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.AdditionalYearlyIncome)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty lgap">
@Html.TextBoxFor(m => m.AdditionalYearlyIncome, new { placeholder = @Resources.AdditionalYearlyIncome })
<span>$</span>
</div>
@Html.ValidationMessageFor(model => model.AdditionalYearlyIncome, "", new { @class = "text-danger" })
</div>
</div> <!-- Row End Here -->
</div>
</div> <!-- Form tab End -->
<div class="frm-bx">
<h2>Save your information</h2>
<div class="frm-gap">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.Email)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.TextBoxFor(m => m.Email, new { placeholder = @Resources.Email })
</div>
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.UserName)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.TextBoxFor(m => m.UserName, new { placeholder = @Resources.Username })
</div>
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="inp-sty1">
@Html.LabelFor(m => m.Password)
</div>
</div>
<div class="col-md-8 col-sm-8 col-xs-12">
<div class="inp-sty">
@Html.PasswordFor(m => m.Password, new { placeholder = @Resources.Password })
</div>
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div> <!-- Form tab End -->
</div> <!-- col-md-8 End -->
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="chklst">
<ul>
<li>
<img src="~/Content/assets/images/stopwatch.svg" alt="">
<p>Check your rate in<br> 5 minutes</p>
</li>
<li>
<img src="~/Content/assets/images/percent.svg" alt="">
<p>Get lower rates</p>
</li>
<li>
<img src="~/Content/assets/images/wallet.svg" alt="">
<p>Single monthly payments</p>
</li>
<li>
<img src="~/Content/assets/images/calendar2.svg" alt="">
<p>Fixed terms—3 or 5 years*</p>
</li>
</ul>
</div>
</div>
<div class="clear"></div>
<div class="chl-txt">
<p>The following documents contain important information, including your agreement to do business with<br> <span>Lendy</span> electronically. <span>By clicking the box below, you confirm that you agree by electronic signature to:</span></p>
</div>
<label class="checke">
the Credit Report Authorization, Terms of Use and Electronic Consent, Data Terms of Use & Prosper privacy policies.
@Html.CheckBoxFor(m => m.AcceptCreditReportTerms)
<span class="checkmark"></span>
</label>
@Html.ValidationMessageFor(model => model.AcceptCreditReportTerms, "", new { @class = "text-danger" })
<input type="submit" class="btncus" value="Check your rates" />
</div>
}
</div>

尝试更新BeginForm,使其看起来像这样。。。

Html.BeginForm("Index", "Home", FormMethod.Post)

我注意到,您正在使用带有if条件的">ModelState.IsValid"属性和if条件中的所有代码检查控制器方法中的模型状态是否有效。

通过在该位置添加断点进行检查,以检查您提交的表单模型此时是否有效。如果false,则不会根据您的逻辑保存您的数据。

另一件事是,在使用">ModelState.IsValid"时,要确保您没有为特定字段发布无效数据,就像您在视图模型中为十进制字段添加了验证一样,并且在表单中,您为该特定字段使用了"TextBoxFor"标记帮助程序,该标记帮助程序接受任何字符,对其他数据类型也是如此,因此请确保将正确的数据发布到控制器,否则">ModelState.IsValid"将为false。

相关内容

  • 没有找到相关文章

最新更新