asp.net MVC 4通过不同形式的多个职位 [英] asp.net MVC 4 multiple post via different forms

查看:87
本文介绍了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"]

它不工作。

我将AP preciate任何帮助。

I will appreciate any help.

感谢。

推荐答案

在一个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:

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

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

}

MemberController.cs

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,你通过控制器名称没有控制器附:


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天全站免登陆