ASP.NET MVC 如何实现多个表单验证和提交

Dick_Xie 2010-04-23 05:34:06
比如说有这么一个页面,包含了用户注册和登录,需要分别验证输入和提交,使用ASP.NET MVC应如何实现?
...全文
1013 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
challenger_chen 2010-04-29
  • 打赏
  • 举报
回复
建议分开来做
cphackers 2010-04-29
  • 打赏
  • 举报
回复
如果确实需要,可以用AJax做
Im_Sorry 2010-04-26
  • 打赏
  • 举报
回复
up!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lyvscf 2010-04-26
  • 打赏
  • 举报
回复
多个表单对应多个form
Dream_Hunter_ 2010-04-26
  • 打赏
  • 举报
回复
mvc有自带的验证控件
另外也可以在form的action指向的action里做判断,再显示页面
Dick_Xie 2010-04-26
  • 打赏
  • 举报
回复
但如果确实有这样的需求,如何处理,提提建议,谢谢。
sjt000 2010-04-26
  • 打赏
  • 举报
回复
每次操作只有一个啊~
手抓宝 2010-04-26
  • 打赏
  • 举报
回复
不建议放在一个页面中
Dick_Xie 2010-04-26
  • 打赏
  • 举报
回复
怎么去处理多个“Html.ValidationSummary”呢?
zhanglei411521 2010-04-24
  • 打赏
  • 举报
回复
这个问题不是很难吧,一个网面只能有一个from ,但每个控件都一个ValidationGroup属性,属于一组的控件或验证控件和提交按钮,把他们的ValidationGroup值设相同的名字,这样多个提交,都互不干涉了。
Dick_Xie 2010-04-23
  • 打赏
  • 举报
回复
Controll:
public ActionResult RegisterAndLogOn()
{
return View();
}

[HttpPost]
public ActionResult LogOn2(string userName, string password, bool rememberMe, string returnUrl)
{
if (ModelState.IsValid)
{
if (ValidateLogOn(userName, password))
{
if (MembershipService.ValidateUser(userName, password))
{
FormsService.SignIn(userName, rememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("logonForm", "The user name or password provided is incorrect.");
}
}
}

// If we got this far, something failed, redisplay form
return View("RegisterAndLogOn");
}

[HttpPost]
public ActionResult Register2(string userName, string email, string password, string confirmPassword)
{
if (ModelState.IsValid)
{
if (ValidateRegistration(userName, email, password, confirmPassword))
{

// Attempt to register the user
int createStatus = MembershipService.CreateUser(userName, password, email);

if (createStatus > 9999)
{
FormsService.SignIn(userName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("registerForm", AccountValidation.ErrorCodeToString(createStatus));
}
}
}

// If we got this far, something failed, redisplay form
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View("RegisterAndLogOn");
}

#endregion
Dick_Xie 2010-04-23
  • 打赏
  • 举报
回复
代码贴上,请帮忙提点建议,主要是如何分别处理表单验证,两个form会有同名的表单项:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Register And LogOn
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Register And LogOn</h2>

<div style="float: left; width: 400px;">
<% using (Html.BeginForm("Register2", "Account", FormMethod.Post, new { id = "registerForm" }))
{ %>
<%= Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.", new { id = "RegisterValidationSummary" })%>
<div>
<fieldset>
<legend>Account Information</legend>

<div class="editor-label">
<%= Html.Label("UserName") %>
</div>
<div class="editor-field">
<%= Html.TextBox("UserName") %>
<%= Html.ValidationMessage("UserName", "*") %>
</div>

<div class="editor-label">
<%= Html.Label("Email") %>
</div>
<div class="editor-field">
<%= Html.TextBox("Email") %>
<%= Html.ValidationMessage("Email", "*") %>
</div>

<div class="editor-label">
<%= Html.Label("Password") %>
</div>
<div class="editor-field">
<%= Html.Password("Password") %>
<%= Html.ValidationMessage("Password", "*") %>
</div>

<div class="editor-label">
<%= Html.Label("ConfirmPassword") %>
</div>
<div class="editor-field">
<%= Html.Password("ConfirmPassword") %>
<%= Html.ValidationMessage("ConfirmPassword", "*") %>
</div>

<p>
<input id="register" type="submit" value="Register" />
</p>
</fieldset>
</div>
<% } %>
</div>
<div style="float: right; width: 280px">
<% using (Html.BeginForm("LogOn2", "Account", FormMethod.Post, new { id = "logonForm" }))
{ %>
<%= Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.", new { id = "LogonValidationSummary" })%>
<div>
<fieldset>
<legend>Log On</legend>

<div class="editor-label">
User Name
</div>
<div class="editor-field">
<%= Html.TextBox("UserName") %>
<%= Html.ValidationMessage("UserName", "*")%>
</div>

<div class="editor-label">
Password
</div>
<div class="editor-field">
<%= Html.Password("Password")%>
<%= Html.ValidationMessage("Password", "*")%>
</div>

<div class="editor-label">
<%= Html.CheckBox("RememberMe") %>
<%= Html.Label("RememberMe") %>
</div>

<p>
<input id="logon" type="submit" value="Log On" />
</p>
</fieldset>
</div>
<% } %>
</div>
<div style="clear: both"></div>

</asp:Content>
wanghuaide 2010-04-23
  • 打赏
  • 举报
回复
分别放在二个from里,form 的action="***.aspx"
wuyq11 2010-04-23
  • 打赏
  • 举报
回复
nwxhl 2010-04-23
  • 打赏
  • 举报
回复
不是很了解mvc 虽然是2.0了。

大概知道是 post 到 model 类里面直接接受数据验证就可以了。至于你要做什么操作,我想都应该可以的。
不明白你为何要提交多个表单

注册完,直接都登陆了。mvc 默认生成该程序。
Dick_Xie 2010-04-23
  • 打赏
  • 举报
回复
是不是问题太傻了呢?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧