ASP.NET MVC:如何验证 ViewModel 中包装的模型?

发布于 2024-09-05 22:11:55 字数 1176 浏览 6 评论 0原文

对于我网站的登录页面,我想列出我网站的最新新闻,并显示一些字段以供用户登录。所以我想我应该制作一个登录视图模型 - 我称之为 LoginVM< /代码>。

LoginVM 包含用于登录字段的 Login 模型和用于新闻列表的 List

这是 Login 模型:

public class Login
{

    [Required(ErrorMessage="Enter a username.")]
    [DisplayName("Username")]
    public string Username { get; set; }

    [Required(ErrorMessage="Enter a password.")]
    [DataType(DataType.Password)]
    [DisplayName("Password")]
    public string Password { get; set; }

}

这是 LoginVM 视图模型:

public class LoginVM
{
    public Login login { get; set; }
    public List<NewsItem> newsItems { get; set; }
}

这就是我陷入困境的地方。在我的登录控制器中,我传递了一个 LoginVM

[HttpPost]
public ActionResult Login(LoginVM model, FormCollection form)
{
    if (ModelState.IsValid)
    {
        // What?

在代码中,我检查 ModelState 是否有效,如果视图模型实际上是 Login 模型,但现在是 LoginVM ,那么这会正常工作> 根本没有验证属性。

如何让 LoginVM “遍历”其成员以验证所有成员?我以这种方式使用 ModelState 是否做了根本错误的事情?

For the login page of my website I would like to list the latest news for my site and also display a few fields to let the user log in. So I figured I should make a login view model - I call this LoginVM.

LoginVM contains a Login model for the login fields and a List<NewsItem> for the news listing.

This is the Login model:

public class Login
{

    [Required(ErrorMessage="Enter a username.")]
    [DisplayName("Username")]
    public string Username { get; set; }

    [Required(ErrorMessage="Enter a password.")]
    [DataType(DataType.Password)]
    [DisplayName("Password")]
    public string Password { get; set; }

}

This is the LoginVM view model:

public class LoginVM
{
    public Login login { get; set; }
    public List<NewsItem> newsItems { get; set; }
}

This is where I get stuck. In my login controller, I get passed a LoginVM.

[HttpPost]
public ActionResult Login(LoginVM model, FormCollection form)
{
    if (ModelState.IsValid)
    {
        // What?

In the code I'm checking whether ModelState is valid and this would work fine if the view model was actually the Login model, but now it's LoginVM which has no validation attributes at all.

How do I make LoginVM "traverse" through its members to validate them all? Am I doing something fundamentally wrong using ModelState in this manner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天生の放荡 2024-09-12 22:11:55

在你的 ViewModel 中尝试这样做:

public class LoginVM
{
    [Required]
    public Login login { get; set; }
    public List<NewsItem> newsItems { get; set; }
}

In your ViewModel try doing this:

public class LoginVM
{
    [Required]
    public Login login { get; set; }
    public List<NewsItem> newsItems { get; set; }
}
怀念你的温柔 2024-09-12 22:11:55

或者,如果 newsItems 用于显示目的,并且不需要验证,那么您可以仅将 Login 模型传递到您的操作方法。

[HttpPost]
public ActionResult Login([Bind(Prefix = "Login")]Login model)
{
    if (!Model.IsValid)
}

您还需要在视图中使用 EditFor 和 LabelFor 帮助器。

<%= Html.TextBoxFor(m => m.Login.Username) %>

Alternatively if the newsItems are for display purposes, and don't need validating, then you can pass through just the Login model to your action method.

[HttpPost]
public ActionResult Login([Bind(Prefix = "Login")]Login model)
{
    if (!Model.IsValid)
}

You will also want to use the EditFor and LabelFor helpers in your View.

<%= Html.TextBoxFor(m => m.Login.Username) %>
悍妇囚夫 2024-09-12 22:11:55
i'd like give you a modal solution:

 @if (Request.IsAuthenticated)

              {
                  <li class="span3">
                      <a href="Home/AboutMe" role="button" class="btn" data-toggle="modal">[@Membership.GetUser().UserName]</a>
                      </li>
                      <li>@Html.ActionLink("logoff","LogOff","Account")</li>
                 }
              else
              {
                   <li class="span3">
                      <a href="#myModal" id="Login" role="button" class="btn" data-toggle="modal" >Login</a>
                   </li>
              }

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel"> Login </h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="control-group">
                                <label class="control-label" for="inputEmail">Email</label>
                                <div class="controls">
                                    <input type="text" id="inputEmail" placeholder="Email">
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="inputPassword">Password</label>
                                <div class="controls">

                                    <input type="password" id="inputPassword" placeholder="Password">
                                </div>
                            </div>
                            <div class="control-group">
                                <div class="controls">
                                    <label class="checkbox">
                                        <input type="checkbox">
                                        Remember me
                                    </label>
                                    <button type="submit" class="btn">Sign in</button>
                                </div>
                            </div>
                        </form>
                    </div>
i'd like give you a modal solution:

 @if (Request.IsAuthenticated)

              {
                  <li class="span3">
                      <a href="Home/AboutMe" role="button" class="btn" data-toggle="modal">[@Membership.GetUser().UserName]</a>
                      </li>
                      <li>@Html.ActionLink("logoff","LogOff","Account")</li>
                 }
              else
              {
                   <li class="span3">
                      <a href="#myModal" id="Login" role="button" class="btn" data-toggle="modal" >Login</a>
                   </li>
              }

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel"> Login </h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="control-group">
                                <label class="control-label" for="inputEmail">Email</label>
                                <div class="controls">
                                    <input type="text" id="inputEmail" placeholder="Email">
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="inputPassword">Password</label>
                                <div class="controls">

                                    <input type="password" id="inputPassword" placeholder="Password">
                                </div>
                            </div>
                            <div class="control-group">
                                <div class="controls">
                                    <label class="checkbox">
                                        <input type="checkbox">
                                        Remember me
                                    </label>
                                    <button type="submit" class="btn">Sign in</button>
                                </div>
                            </div>
                        </form>
                    </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文