Net Core并且遇到了一个减速带。我希望获得@ Html.DropDownListFor的选定值,并将字符串值传递给开关以继续显示表单的其余部分。
我在堆栈上看到了很多@onchange的方法,但没办法将变量的数据传递给@switch 模型
public class RegisterViewModel
{
public int ID { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} 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; }
// New guild yes or no
[Required]
[Display(Name = "Are you registering a NEW GUILD?")]
public string RegisterNewGuild { get; set; }
// If yes
[Display(Name ="Are you the Guild Leader of the guild your are trying to register?")]
public string GuildLeader { get; set; }
//if yes
[Display(Name = "As the Guild Leader, What is your character's name?")]
public string GuildLeaderName { get; set; }
// if NOT THE guild leader and register new guild was marked yes.
[Display(Name = "As the Guild Member who is registering your guild, what is your Guild Leader's character name?")]
public string GuildLeaderNameViaMember { get; set; }
// get the persons character name who is registering
[Display(Name = "What is your character's name?")]
public string MemberName { get; set; }
[Display(Name = " What is your Guild's name?")]
public string GuildName { get; set; }
}
public enum GuildLeaderEnum
{
Yes,
No
}
public enum RegisterNewGuildEnum
{
Yes,
No
}
public enum GuildNameEnum
{
Test1,
Test2
}
查看
@model RegisterViewModel
@{
ViewData["Title"] = "Register";
}
<h2>@ViewData["Title"].</h2>
<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">
<h4>Create a new account.</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Password" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div id="RegisterNewGuild" class="form-group">
<label asp-for="RegisterNewGuild" class="col-md-2 control-label"></label>
<div class="col-md-offset-2 col-md-10">
@Html.DropDownListFor(m => m.RegisterNewGuild,
new SelectList(Enum.GetValues(typeof(RegisterNewGuildEnum))),
"")
</div>
</div>
@switch ()// This is where i need help to Capture the string from the select list above)
{
case "Yes":
<div id="GuildLeader" class="form-group">
<label asp-for="GuildLeader" class="col-md-2 control-label"></label>
<div class="col-md-offset-2 col-md-10">
@Html.DropDownListFor(m => m.GuildLeader,
new SelectList(Enum.GetValues(typeof(GuildLeaderEnum))),
"")
</div>
</div>
switch ()// This is where i need help to Capture the string from the select list above)
{
case "Yes":
<div id="GuildLeaderName" class="form-group">
<label asp-for="GuildLeaderName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="GuildLeaderName" class="form-control" />
</div>
</div>
break;
case "No":
<div id="GuildLeaderNameViaMember" class="form-group">
<label asp-for="GuildLeaderNameViaMember" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="GuildLeaderNameViaMember" class="form-control" />
</div>
</div>
break;
}
break;
case "No":
<div id="MemberName" class="form-group">
<label asp-for="MemberName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="MemberName" class="form-control" />
</div>
</div>
<div id="GuildName" class="form-group">
<label asp-for="GuildName" class="col-md-2 control-label"></label>
<div class="col-md-10">
@Html.DropDownListFor(m => m.GuildName,
new SelectList(Enum.GetValues(typeof(GuildNameEnum))),
"")
</div>
</div>
break;
}
控制器
// GET: /Account/Register
[HttpGet]
[AllowAnonymous]
public IActionResult Register( string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null )
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
//var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
//var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
//await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
// $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}