asp.net MVC 4 通过不同形式多次发布 [英] asp.net MVC 4 multiple post via different forms

查看:26
本文介绍了asp.net MVC 4 通过不同形式多次发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我明白了

if (IsPost){   //do stuff }

检查该页面上的所有发布方法.但是,我有 2 个不同的表单发布 2 个不同的信息.这些是登录表单和注册表单.

checks all post methods on that page. However, I have 2 different forms posting 2 different information. These are a login form and a register form.

有什么方法可以根据哪种形式检查 IsPost?例如,

Is there a way I can check IsPost based on which form? For example,

if(Login.IsPost){ //do stuff }

但是我将如何定义登录变量?我的表格看起来像:

but how would I define the Login variable? My form looks like:

<form id="Login" method = "POST">

我试过了:

var Login = Form.["Login"]

它没有用.

我将不胜感激.

谢谢.

推荐答案

在 MVC 视图中,您可以根据需要拥有包含多个字段的多个表单.为简单起见,请使用单个视图模型,其中包含页面上每个表单所需的所有属性.请记住,您只能访问您提交的表单中的表单域数据.因此,如果您在同一页面上有登录表单和注册表单,您可以这样做:

In an MVC view, you can have as many forms with as many fields as you need. To keep it simple, use a single view model with all the properties you need on the page for every form. Keep in mind that you will only have access to the form field data from the form that you submit. So, if you have a login form and registration form on the same page you would do it like this:

登录注册ViewModel.cs

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; }
}

您的视图名称.cshtml

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' />

}

成员控制器.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 时,您传递的控制器名称没有附加Controller":


Do not forget, when calling BeginForm, you pass the Controller name without "Controller" attached:

@using (Html.BeginForm("Login", "Member", FormMethod.Post, new {}))

代替:

@using (Html.BeginForm("Login", "MemberController", FormMethod.Post, new {}))

这篇关于asp.net MVC 4 通过不同形式多次发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆