0

自分でコーディングしたビューでエラーログイン通知を渡したいのですが、方法がわかりません。@Html.ValidationMessageFor(model => model.UserName)andまたは別のラベルに入れたい(の代わりに@Html.ValidationMessageFor(model => model.Password)使用するのは正しいですか?)@Html.ValidationMessage()@Html.ValidationMessageFor()

ここに私のモデルがあります:

public class User 
{
    public int UserId { get; set; }

    [Required]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }
}

ここに私のコントローラーがあります:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User p)
{
    if (ModelState.IsValid)
    {
        User item = db.Authenticate(p);

        if (item != null) // if item is not null, the login succeeded
        {
            return RedirectToAction("Main", "Home");
        }
    }
    string error = "Incorrect user name or password."; //  I don't know how to pass this
    return View(); //login failed
}

ここに私の見解があります:

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>User</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <p>
            <input type="submit" value="Login" />
        </p>
    </fieldset>
}
4

2 に答える 2