asp.net MVC 4 通过不同形式多次发布

编程入门 行业动态 更新时间:2024-10-15 22:28:16
本文介绍了asp 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 通过不同形式多次发布

本文发布于:2023-10-11 10:15:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形式   asp   net   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!