繁体   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