JQuery验证消息显示在页面加载上

时间:2015-07-05 16:51:30

标签: c# jquery asp.net-mvc

我有以下观点:

@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);
    }

如何停止在加载时显示验证消息,我需要做什么?

1 个答案:

答案 0 :(得分:1)

从GET方法中删除NewClubInterestsViewModel model参数。它应该只是

public ActionResult NewInterests(int id)

模型绑定过程的第一步是初始化参数的实例,然后根据表单值,路由值,查询字符串值等设置其属性。在您的情况下,没有要设置的值,因此模型的属性是它们的默认值,但由于您的属性上有验证属性,验证失败并且错误被添加到ModelState,然后显示在视图中。