提交按钮不调用MVC 5中的方法 [英] Submit button not calling the method in MVC 5
问题描述
您好我正在为客户创建一个简单的登录注册页面。由于我是MVC5的新手,我遵循了一些教程并创建了一个登录页面。它工作正常,但在创建注册表单并突然进行一些更改后,它只是刷新页面。我撤消所有更改,但结果是一样的。
以下是Controller的代码:
Hi I am creating a simple login registration page for customers. As I am new to MVC5, I followed some tutorial and created a login page. It was working fine but after creating registration form and making some changes suddenly it is just refreshing the page. I undo all the changes but the result is same.
Here is the code for Controller:
using MyWaayEcommerce.Models;
using MyWaayEcommerce.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;
namespace MyWaayEcommerce.Controllers
{
public class AuthController : Controller
{
// GET: Auth
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(UserLoginVM lv)
{
if (ModelState.IsValid)
{
UserManager um = new UserManager();
if (um.IsEmailExist(lv.UserName))
{
FormsAuthentication.SetAuthCookie(lv.UserName, false);
return RedirectToAction("Index", "Customers");
}
else
{
return RedirectToAction("Index", "Customers");
//ModelState.AddModelError("", "Email or Password does not match");
}
}
return View();
}
}
}
UserManagerModel代码:
UserManagerModel Code:
using MyWaayEcommerce.Models;
using MyWaayEcommerce.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;
namespace MyWaayEcommerce.Controllers
{
public class AuthController : Controller
{
// GET: Auth
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(UserLoginVM lv)
{
if (ModelState.IsValid)
{
UserManager um = new UserManager();
if (um.IsEmailExist(lv.UserName))
{
FormsAuthentication.SetAuthCookie(lv.UserName, false);
return RedirectToAction("Index", "Customers");
}
else
{
return RedirectToAction("Index", "Customers");
//ModelState.AddModelError("", "Email or Password does not match");
}
}
return View();
}
}
}
用户登录视图型号:
User LoginView Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MyWaayEcommerce.Models.ViewModels
{
public class UserLoginVM
{
[Required(ErrorMessage = "Email is required")]
[Display(Name ="Email")]
public string UserName { get; set; }
[Required(ErrorMessage = "Password is required")]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessage = "Street Address is Required")]
[Display(Name = "Street Address 1")]
public string Street1 { get; set; }
[Display(Name = "Street Address 1")]
public string Street2 { get; set; }
[Required(ErrorMessage = "City Name is Required")]
[Display(Name = "City")]
public string City { get; set; }
[Required(ErrorMessage = "Province Name is Required")]
[Display(Name = "Province")]
public string Province { get; set; }
[Required(ErrorMessage = "Country Name is Required")]
[Display(Name = "Country")]
public string Country { get; set; }
[Required(ErrorMessage = "Postal Code is Required")]
[Display(Name = "Postal Code")]
public string Postal { get; set; }
[Required(ErrorMessage = "Phone Number is Required")]
[Display(Name = "Contact Number")]
public string ContactNumber { get; set; }
}
}
登录视图名为Index.cshtml
Login View named Index.cshtml
@model MyWaayEcommerce.Models.ViewModels.UserLoginVM
@{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Login</h2>
@using (Html.BeginForm("Login", "Auth", FormMethod.Post, new { id = "submitForm" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
<br />
@Html.PasswordFor(model => model.Password, new { @class = "form-control" } )
<br />
</div>
<input type="submit" value="Login" class="btn btn-default" />
}
我的尝试:
我尝试通过搜索提交按钮而不是调用方法来修复它,并提出了表单标签参数但结果却相同
What I have tried:
I tried to fix that by searching submit button not calling method and came up with form tag parameters but results yet same
推荐答案
我想我看到了你的问题。
在你的Html.Begin表格中你表明你的目标是行动登录控制器Auth。
I think i see your issue.
In your Html.Begin Form you indicate you are targeting action Login in controller Auth.
@using (Html.BeginForm("Login", "Auth", FormMethod.Post, new { id = "submitForm" }))<
在上面,Login是你的Action,Auth是你的控制器。动作意味着它是控制器的方法。
所以在你的AuthController中你有一个Index动作但没有登录动作
In the above, Login is your Action and Auth is your controller. Action meaning it is the method of a controller.
So in your AuthController you have an Index action but no Login action
public class AuthController : Controller
{
[HttpPost]
public ActionResult Index(UserLoginVM lv)
{
if (ModelState.IsValid)
{
UserManager um = new UserManager();
if (um.IsEmailExist(lv.UserName))
{
FormsAuthentication.SetAuthCookie(lv.UserName, false);
return RedirectToAction("Index", "Customers");
}
else
{
return RedirectToAction("Index", "Customers");
//ModelState.AddModelError("", "Email or Password does not match");
}
}
return View();
}
}
您需要从索引中更改上述操作
到登录
然后你的代码应该工作(我没有运行它。)
You need to change the above action from Index
to Login
and then your code should work (i didn't run it).
这篇关于提交按钮不调用MVC 5中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!