簡體   English   中英

jQuery驗證消息在頁面加載時顯示

[英]JQuery Validation message shows on page load

我有以下觀點:

@model GRCWebApp.ViewModels.NewClubInterestsViewModel

@{
ViewBag.Title = "Add Club Interests";
}
<div class="col-md-10 col-offset-md-1">
<h2 class ="text-success">Add Club Interests for @Html.DisplayFor(model => model.Name)</h2>
</div>

@using (Html.BeginForm("NewInterests", "Club", FormMethod.Post))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    <div class="row">
        <div class="col-md-8">
            <div class="well bs-component">
                @Html.ValidationSummary(true, "", new { @class = "text-danger"   })
                @Html.HiddenFor(x => Model.ClubId)
                <div class="form-group">
                    <div class="col-md-10 col-md-offset-1">
                        <h3>Tick the areas your club is interested/runs events in</h3>
                    </div>
                </div>
                <div class="col-md-offset-1">


                    @for (int i = 0; i < Model.ClubTypeInterests.Count();  i++)
                    {
                        <div>
                            @Html.HiddenFor(x => Model.ClubTypeInterests[i].InterestId)
                            @Html.CheckBoxFor(x => Model.ClubTypeInterests[i].selected)
                            @Html.LabelFor(x => Model.ClubTypeInterests[i].InterestName, Model.ClubTypeInterests[i].InterestName)
                        </div>
                    }
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-offset-1 col-md-12">
                            <input type="submit" name="Submit" value="Next" class="btn btn-success btn-lg" />&nbsp;to setup Online membership&nbsp;
                            <input type="submit" name="Submit" value="Complete" class="btn btn-warning btn-sm" />&nbsp;to just provide online event entries
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

}

@using (Html.BeginForm("AddInterest", "Club"))
{
@Html.AntiForgeryToken()


<hr />
<div class="row">
    <div class="col-md-8">
        <div class="well bs-component">
            <div class="form-group">
                <div class="col-md-12 col-md-offset-1">
                    <h3>Not listed? - Add extras here</h3>
                </div>
            </div>
            @Html.HiddenFor(model => model.ClubTypeId)
            @Html.HiddenFor(model => model.ClubId)
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-inline">
                <div class="form-group">
                    <div class="row col-md-offset-1 col-md-11">
                        <div class="col-md-4">
                            @Html.LabelFor(model => model.InterestName, htmlAttributes: new { @class = "control-label" })
                            @Html.EditorFor(model => model.InterestName, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.InterestName, "", new { @class = "text-danger" })
                        </div>
                        <div class="col-md-5">
                            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label" })
                            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
                        </div>
                        <input type="submit" value="Add" class="btn btn-info" style="margin-top: 22px" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
}


@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

加班頁面將加載以第二種形式AddInterest顯示的InterestName的驗證消息。

整個表單的Get方法是:

        [HttpGet]
    public ActionResult NewInterests(NewClubInterestsViewModel model, int id)
    {
        //Find the Club and then pass its id to the ViewModel
        var club = db.Clubs.Find(id);
        //model.ClubId = club.ClubId ;
        //Interests List         
        IEnumerable<Interest> allExistingInterests;
        //Generate a list of the interests that apply to that type of club
        using (ApplicationDbContext context = new ApplicationDbContext())
        {
            allExistingInterests = context.Interests
                .Where(s => s.ClubTypeId == club.ClubTypeId)
                .OrderBy(s => s.InterestName)
                .ToList();
        }
        IEnumerable<int> clubInterestIds = club.ClubInterests.Select(x => x.InterestId).ToList();
        //Generate the ViewModel with the appropriate Interests
        var viewModel = new NewClubInterestsViewModel
        {
            ClubId = club.ClubId,
            Name = club.Name,
            ClubTypeId = club.ClubTypeId,
            ClubTypeInterests =
                allExistingInterests.Select(
                    x => new ClubInterestsViewModel { InterestId = x.InterestId, InterestName = x.InterestName, selected = clubInterestIds.Contains(x.InterestId) }).ToArray()
        };
        return View(viewModel);
    }

我需要怎么做才能停止顯示加載時的驗證消息?

從GET方法中刪除NewClubInterestsViewModel model參數。 應該只是

public ActionResult NewInterests(int id)

模型綁定過程的第一步是初始化參數的實例,然后根據表單值,路由值,查詢字符串值等設置其屬性。在這種情況下,沒有要設置的值,因此您的屬性模型是其默認值,但是由於屬性上具有驗證屬性,因此驗證失敗,並且將錯誤添加到ModelState ,然后將其顯示在視圖中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM