现在我明白了
if (IsPost){ //do stuff }
检查该网页上的所有岗位的方法。 但是,我有2种不同的形式发布2个不同的信息。 这是一个登录表单和登记表。
有没有一种方法,我可以根据其形态上的检查IsPost? 例如,
if(Login.IsPost){ //do stuff }
但我会如何定义登录变量? 我的形式是这样的:
<form id="Login" method = "POST">
我试过了:
var Login = Form.["Login"]
那没起效。
我感谢所有帮助。
谢谢。
在一个MVC视图,因为你需要,你可以有尽可能多的领域尽可能多的形式。 为了保持它的简单,用一个单一的视图模型,你需要在页面上每一个表格的所有属性。 请记住,你将只能访问从表单,您提交表单字段数据。 所以,如果你有相同的页面上的登录表单,并登记表,你会做这样的:
LoginRegisterViewModel.cs
public class LoginRegisterViewModel {
public string LoginUsername { get; set; }
public string LoginPassword { get; set; }
public string RegisterUsername { get; set; }
public string RegisterPassword { get; set; }
public string RegisterFirstName { get; set; }
public string RegisterLastName { get; set; }
}
YourViewName.cshtml
@model LoginRegisterViewModel
@using (Html.BeginForm("Login", "Member", FormMethod.Post, new {})) {
@Html.LabelFor(m => m.LoginUsername)
@Html.TextBoxFor(m => m.LoginUsername)
@Html.LabelFor(m => m.LoginPassword)
@Html.TextBoxFor(m => m.LoginPassword)
<input type='Submit' value='Login' />
}
@using (Html.BeginForm("Register", "Member", FormMethod.Post, new {})) {
@Html.LabelFor(m => m.RegisterFirstName)
@Html.TextBoxFor(m => m.RegisterFirstName)
@Html.LabelFor(m => m.RegisterLastName)
@Html.TextBoxFor(m => m.RegisterLastName)
@Html.LabelFor(m => m.RegisterUsername)
@Html.TextBoxFor(m => m.RegisterUsername)
@Html.LabelFor(m => m.RegisterPassword)
@Html.TextBoxFor(m => m.RegisterPassword)
<input type='Submit' value='Register' />
}
MemberController.cs
[HttpGet]
public ActionResult LoginRegister() {
LoginRegisterViewModel model = new LoginRegisterViewModel();
return view("LoginRegister", model);
}
[HttpPost]
public ActionResult Login(LoginRegisterViewModel model) {
//do your login code here
}
[HttpPost]
public ActionResult Register(LoginRegisterViewModel model) {
//do your registration code here
}
不要忘了,在调用时BeginForm,你通过控制器名称没有“控制器”附:
@using (Html.BeginForm("Login", "Member", FormMethod.Post, new {}))
代替:
@using (Html.BeginForm("Login", "MemberController", FormMethod.Post, new {}))
我刚加载(含有一种形式)的局部视图为每个所需的形式,给每个部分不同的视图模型:
- 多形上一个页面,要求:满足。
- 每份表格上不显眼的Javascript验证:完成。
你应该让每个<form>
点都有自己的模型参数的单独行动。
而不是做一个表单提交的,我们可以做相应的提交按钮的点击一个ajax开机自检。
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { @Id = "Form1" }))
{
}
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { @Id = "Form2" }))
{
}
一旦你分配不同的id属性给每个在你的页面的形式,使用这样的代码:
$(document).ready( function() {
var form = $('#Form1');
$('#1stButton').click(function (event) {
$.ajax( {
type: "POST",
url: form.attr( 'action' ),
data: form.serialize(),
success: function( response ) {
console.log( response );
}
} );
} );
}
重复2日按钮的Click事件同样的事情援引第二形式阿贾克斯后。
此代码使用.serialize()从形式拉出相关数据。
对于未来的参考, jQuery的文档是非常,非常好。
注意:您使用的触发,导致通过AJAX后表单的提交事件的按钮不应该的类型提交 ! 否则,这将总是失败。
Use this example to prevent unnecessary/unintended validation checks while using multiple Form Post with different Controller Actions.
要找什么
- 本质上,这代码利用该模型标志该控制器动作被称为在表单提交的布尔内。
- 注意这里使用了嵌套模式和[是行动登录]和[是行动注册]布尔属性,我使用方法帮助IsActionLogin()和IsActionRegister()设置。 只有一个将在各自控制器动作被调用。
请注意,在模型中sReturnURL财产。 此属性存储一个导航网址,并与登录和注册控制器动作共享。 这将使我们返回到用户离开该页面不必登录之前。
/* Begin Model logic */ public class LoginRegisterModel { public LoginModel LoginModel { get; set; } public RegisterModel RegisterModel { get; set; } public string sReturnURL { get; set; } public bool bIsActionLogin { get; set; } public bool bIsActionRegister { get; set; } public void IsActionLogin() { bIsActionLogin = true; bIsActionRegister = false; } public void IsActionRegister() { bIsActionLogin = false; bIsActionRegister = true; } } public class LoginRegisterModel { public LoginModel LoginModel { get; set; } public RegisterModel RegisterModel { get; set; } public string sReturnURL { get; set; } public bool bIsActionLogin { get; set; } public bool bIsActionRegister { get; set; } public void IsActionLogin() { bIsActionLogin = true; bIsActionRegister = false; } public void IsActionRegister() { bIsActionLogin = false; bIsActionRegister = true; } } public class RegisterModel { [Required] [Display(Name = "Email")] [RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "Email is not valid.")] 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; } [Required] [DataType(DataType.Password)] [Display(Name = "Confirm Password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } } /*End Model logic*/ /*Begin Controller Logic*/ [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginRegisterModel model, string sReturnURL) { model.IsActionLogin(); //flags that you are using Login Action //process your login logic here } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Register(LoginRegisterModel model, string sReturnURL) { model.IsActionRegister(); //flag Action Register action //process your register logic here } /*End Controller Logic*/ /*Begin View Logic*/ @model eCommerce.Models.LoginRegisterModel @{ /*Place this view logic in both the Login.cshtml and Register.cshtml. Now use the last Action called as a Boolean check against your validation messages, so unnecessary validation messages don't show up.*/ bool bLoginCallBack = Model.bIsActionLogin; bool bRegisterCallBack = Model.bIsActionRegister; MvcHtmlString htmlIcoWarn = new MvcHtmlString(" font awesome icon here "); MvcHtmlString htmlIcoHand = new MvcHtmlString(" font awesome icon here "); } @using (Html.BeginForm("Login", "Account", new { sReturnURL = Model.sReturnURL })) { @Html.AntiForgeryToken() if (bLoginCallBack) { MvcHtmlString htmlLoginSummary = Html.ValidationSummary(true); if (!htmlLoginSummary.ToHtmlString().Contains("display:none")) { @:@(htmlIcoWarn)@(htmlLoginSummary) } } @Html.LabelFor(m => m.LoginModel.UserName) @Html.TextBoxFor(m => m.LoginModel.UserName, new { @placeholder = "Email" }) @if (bLoginCallBack) { MvcHtmlString htmlLoginUsername = Html.ValidationMessageFor(m => m.LoginModel.UserName); if (!htmlLoginUsername.ToHtmlString().Contains("field-validation-valid")) { @:@(htmlIcoHand) @(htmlLoginUsername) } } @Html.LabelFor(m => m.LoginModel.Password) @Html.PasswordFor(m => m.LoginModel.Password, new { @placeholder = "Password" }) @if (bLoginCallBack) { MvcHtmlString htmlLoginPassword = Html.ValidationMessageFor(m => m.LoginModel.Password); if (!htmlLoginPassword.ToHtmlString().Contains("field-validation-valid")) { @:@(htmlIcoHand) @(htmlLoginPassword) } } @Html.CheckBoxFor(m => m.LoginModel.RememberMe) @Html.LabelFor(m => m.LoginModel.RememberMe) <button type="submit" class="btn btn-default">Login</button> } @using (Html.BeginForm("Register", "Account", new { sReturnURL = Model.sReturnURL })) { @Html.AntiForgeryToken() if (bRegisterCallBack) { MvcHtmlString htmlRegisterSummary = Html.ValidationSummary(true); if (!htmlRegisterSummary.ToHtmlString().Contains("display:none")) { @:@(htmlIcoWarn)@(htmlRegisterSummary) } } @Html.LabelFor(m => m.RegisterModel.UserName) @Html.TextBoxFor(m => m.RegisterModel.UserName, new { @placeholder = "Email" }) @if (bRegisterCallBack) { MvcHtmlString htmlRegisterUsername = Html.ValidationMessageFor(m => m.RegisterModel.UserName); if (!htmlRegisterUsername.ToHtmlString().Contains("field-validation-valid")) { @:@(htmlIcoHand) @(htmlRegisterUsername) } } @Html.LabelFor(m => m.RegisterModel.Password) @Html.PasswordFor(m => m.RegisterModel.Password, new { @placeholder = "Password" }) @if (bRegisterCallBack) { MvcHtmlString htmlRegisterPassword = Html.ValidationMessageFor(m => m.RegisterModel.Password); if (!htmlRegisterPassword.ToHtmlString().Contains("field-validation-valid")) { @:@(htmlIcoHand) @(htmlRegisterPassword) } } @Html.LabelFor(m => m.RegisterModel.ConfirmPassword) @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword, new { @placeholder = "Confirm Password" }) @if (bRegisterCallBack) { MvcHtmlString htmlRegisterConfirmPassword = Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword); if (!htmlRegisterConfirmPassword.ToHtmlString().Contains("field-validation-valid")) { @:@(htmlIcoHand) @(htmlRegisterConfirmPassword) } <button type="submit" class="btn btn-default">Signup</button> } } /*End View Logic*/