仅验证所需模型

Validate required model only

我在一个视图中使用了两个模型,即登录和注册模型。

public class Login
{
    [Required(ErrorMessage ="User ID Required.")]
    public string UserID { get; set; }
    [Required(ErrorMessage ="Password Required")]
    public string Password { get; set; }

}

public class SignUp
{
    [Required (ErrorMessage ="User ID Required")]
    public string UserID { get; set; }

    [Required (ErrorMessage ="Name Required")]
    public string Name { get; set; }

    [Required (ErrorMessage ="Mail ID Required")]
    public string MailID { get; set; }

    [Required(ErrorMessage ="Password Required")]
    public string Password { get; set; }

    [Required(ErrorMessage ="Confirm Password Required")]
    [Compare (nameof(Password), ErrorMessage ="Password does not match")]
    public string ConfirmPassword { get; set; }         
}

当我单击“登录”按钮时,它会验证两个模型。如何单独验证模型?

在控制器中使用了以下代码

   public ActionResult Index()
{
    return View();
}
[HttpPost]
public ActionResult Index(string Command, Login Login)
{
    if (Command == "SIGNUP")
    {
        return RedirectToAction("Contact");
    }
    else
    {
        if (ModelState.IsValidField("USERID") && ModelState.IsValidField("PASSWORD"))
        {
            return RedirectToAction("About");
        }               
    }
    return View();
}

Index.cshtml 查看代码:

    @using (Html.BeginForm())
 {
   @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="HolderForm">
    <div class="col-md-6">
        <div class="form-horizontal">
            <h4>Login</h4>
            <hr />

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.Login.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br>
                    @Html.ValidationMessageFor(o => o.Login.UserID,"", new {@class= "LoginValidation" } )
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.Login.Password, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD" } })<br>
                    @Html.ValidationMessageFor(o => o.Login.Password,"", new { @class = "LoginValidation" })
                    @ViewBag.Posted
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                   @ViewBag.Posted
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" 
                            value="LOGIN"
                           id="btn_Login"
                           name="Command"
                            class="btn btn-default" />
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-horizontal">
            <h4>SignUp</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger"     })
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.UserID,"", new {  @class = "LoginValidation" } )
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.Name, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "NAME" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.Name, "", new { @class = "LoginValidation" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.MailID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "MAIL ID" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.MailID, "", new     { @class = "LoginValidation" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.Password, new {   htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD", @type = "password" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.Password, "", new { @class = "LoginValidation" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.ConfirmPassword, new {     htmlAttributes = new { @class = "LoginEntry", @placeholder = "CONFIRM PASSWORD",     @type = "password" } })<br>
                    @Html.ValidationMessageFor(o =>    o.SignUp.ConfirmPassword, "", new { @class = "LoginValidation" })
                 </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                        <input type= "submit" value="SIGNUP"                               
                                 id="btn_Login"
                               name="Command"
                                class="btn btn-default" />
                    </div>
                </div>
            </div>
        </div>      
    </div>


}

以上代码放置在使用两个不同模型的视图上。

请帮助并建议我在哪里学习ASP.NET(初级水平)?

您在一个表格中添加 LoginSignUp

@using (Html.BeginForm()){
    ...
}

试试这个

<div class="HolderForm">
 @using (Html.BeginForm())
 {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="col-md-6">
        <div class="form-horizontal">
            <h4>Login</h4>
            <hr />

            <div class="form-group">
                <div class="col-md-10">
                @Html.EditorFor(o => o.Login.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br>
                @Html.ValidationMessageFor(o => o.Login.UserID,"", new {@class= "LoginValidation" } )
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-10">
                @Html.EditorFor(o => o.Login.Password, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD" } })<br>
                @Html.ValidationMessageFor(o => o.Login.Password,"", new { @class = "LoginValidation" })
                @ViewBag.Posted
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
               @ViewBag.Posted
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" 
                        value="LOGIN"
                       id="btn_Login"
                       name="Command"
                        class="btn btn-default" />
            </div>
        </div>
    </div>
</div>
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="col-md-6">
        <div class="form-horizontal">
            <h4>SignUp</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger"     })
            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.UserID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "USER ID" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.UserID,"", new {  @class = "LoginValidation" } )
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.Name, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "NAME" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.Name, "", new { @class = "LoginValidation" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.MailID, new { htmlAttributes = new { @class = "LoginEntry", @placeholder = "MAIL ID" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.MailID, "", new     { @class = "LoginValidation" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.Password, new {   htmlAttributes = new { @class = "LoginEntry", @placeholder = "PASSWORD", @type = "password" } })<br>
                    @Html.ValidationMessageFor(o => o.SignUp.Password, "", new { @class = "LoginValidation" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-10">
                    @Html.EditorFor(o => o.SignUp.ConfirmPassword, new {     htmlAttributes = new { @class = "LoginEntry", @placeholder = "CONFIRM PASSWORD",     @type = "password" } })<br>
                    @Html.ValidationMessageFor(o =>    o.SignUp.ConfirmPassword, "", new { @class = "LoginValidation" })
                 </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type= "submit" value="SIGNUP"                               
                             id="btn_Login"
                           name="Command"
                            class="btn btn-default" />
                </div>
            </div>
        </div>
    </div>      
</div>


}