首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MVC所需的字段不工作

MVC所需的字段不工作
EN

Stack Overflow用户
提问于 2013-09-09 20:53:42
回答 2查看 7.6K关注 0票数 0

有人能发现我做错了什么吗?我尝试了一下我的HTMlValidation摘要和其他一些东西。我觉得这可能与我从我的控制器类返回的视图有关。

代码语言:javascript
复制
-- Model
 public class Login
{
    [Required(ErrorMessage = "First Name is required")]
    [Display(Name = "First Namex")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [DataType(DataType.Password)]
    [Display(Name = "Passwordx")]
    public string Password { get; set; }
}

  -- Controller
[HttpPost]
    public ActionResult Login(string FirstName, string Password)
    {
        if (ModelState.IsValid)
        {
            bool validLogin = new UserBAL().ValidateUser(FirstName, Password);
            {
                if (validLogin == true)
                {
                    return RedirectToAction("Index", "Invoice");
                }
                else
                {
                  return RedirectToAction("Index");
                  //  ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
        }
        return View();
    }

 --View 
@using (Html.BeginForm("Login", "Home"))  
{ @Html.ValidationSummary(true)
<div>
    <fieldset>   
        <legend>Login</legend>       
        <div class ="fields">              
          @Html.LabelFor(u => u.FirstName)
          </div>
          @Html.TextBoxFor(u => u.FirstName)
          @Html.ValidationMessageFor(u => u.FirstName)  <br />       

        <div class ="fields">            
         @Html.LabelFor(u => u.Password)
        </div>
         @Html.PasswordFor(u => u.Password) <br />                 
        <input type="submit" value="Log In" />
    </fieldset>
</div>   

}

代码语言:javascript
复制
[HttpPost]
    public ActionResult Login(EIAS.Models.Login login)
    {
        if (ModelState.IsValid)
        {
            bool validLogin = new UserBAL().ValidateUser(login);
            {
            if (validLogin == true)
                {
                    return RedirectToAction("Index", "Invoice");
                }
                else
                {
                  return RedirectToAction ("Index");
                  //  ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
        }
        return View();
    }  
EN

回答 2

Stack Overflow用户

发布于 2013-09-10 02:20:00

if (ModelState.IsValid)中的MVC验证模型,并且在操作中没有接收到该模型:

代码语言:javascript
复制
public ActionResult Login(string FirstName, string Password)

将操作参数更改为:

代码语言:javascript
复制
public ActionResult Login(Login model)

此外,要在客户端进行验证,请检查:

  • 如果包含jquery,则验证插件(js)
  • 检查您的web.config,键ClientValidationEnabledUnobtrusiveJavaScriptEnabled ir为true。

check this link

票数 5
EN

Stack Overflow用户

发布于 2013-09-09 20:55:32

您必须将模型作为登录操作的参数

代码语言:javascript
复制
public ActionResult Login()
{
    // This will return the view with the form that you have above
    return View();
}

[HttpPost]
public ActionResult Login(Login login)
{
    // this is what your form will post too
    if (ModelState.IsValid)
    {
        bool validLogin = new UserBAL().ValidateUser(login.FirstName, login.Password);
        {
            if (validLogin == true)
            {
                return RedirectToAction("Index", "Invoice");
            }
            else
            {
              return RedirectToAction("Index");
              //  ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }
    }
    return View();
}

在视图中才有

代码语言:javascript
复制
@using (Html.BeginForm())

下面是关于MVC Razor表单的链接:http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/

试试看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18706846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档