Kendoui |网格|网格中的服务器端验证

时间:2014-06-18 03:50:59

标签: asp.net-mvc-4 entity-framework-5 kendo-ui-mvc

我使用带有ClientTemplate的kendoui网格在网格的每一行上显示文本框。 我需要在点击网格外部的按钮时在网格的每个空文本框上显示验证消息,这将实际发布数据。

查看

@(Html.Kendo().Grid<MMM.Lumos.Entities.CustomEntities.OrganizationRiskViewModel>()
                .Name("OrgRiskGrid")
                .DataSource(dataSource => dataSource.Ajax()
                    .Model(model =>
                    {
                        model.Id(m => m.RiskId);
                        model.Id(m => m.RiskTierId);
                        model.Id(m => m.RiskTierKey);
                        model.Id(m => m.RiskKey);
                    })
                    .Read(read => read.Action("GetRiskType", "RiskTier").Data("getRiskTier"))
                    .Events(events =>
                        events.Error("error"))
                )
                .Columns(columns =>
                {
                    columns.Bound(c => c.RiskName).Width(50);
                    columns.Bound(c => c.ATPTestMix).ClientTemplate(Html.Kendo().IntegerTextBox().Name("ATPTestMix").Min(0).HtmlAttributes(new { value = "", style = "width: 50px;" }).ToClientTemplate().ToHtmlString()).Width(60);
                    columns.Bound(c => c.VITestMix).ClientTemplate(Html.Kendo().IntegerTextBox().Name("VITestMix").Min(0).HtmlAttributes(new { value = "", style = "width: 50px;" }).ToClientTemplate().ToHtmlString()).Width(60);
                    columns.Bound(c => c.SMTestMix).ClientTemplate(Html.Kendo().IntegerTextBox().Name("SMTestMix").Min(0).HtmlAttributes(new { value = "", style = "width: 50px;" }).ToClientTemplate().ToHtmlString()).Width(60);
                })
            )

模型

public class OrganizationRiskViewModel
    {        
        public int OrganizationId { get; set; }

        public short RiskTierId { get; set; }

        public string RiskTierName { get; set; }

        public short RiskId { get; set; }

        public string RiskName { get; set; }

        [Required(ErrorMessage="ATP Test Mix is mandatory")]
        public short ATPTestMix { get; set; }

        [Required(ErrorMessage = "ATP Test Mix is mandatory")]
        public short SMTestMix { get; set; }

        [Required(ErrorMessage = "ATP Test Mix is mandatory")]
        public short VITestMix { get; set; }

        public string RiskTierKey { get; set; }

        public string RiskKey { get; set; }
    }

我尝试在Grid绑定到的模型上设置数据注释,但不幸的是它没有用。

如果有人有解决办法,请告诉我。

1 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
    $(function () {
        var form = $('#yourFormName');
        form.data('validator').settings.ignore = ''; // default is ":hidden".
    });
</script>

//设置DataAnnotation属性

public IList<SelectListItem> SecretQuestion1IdList { get; set; }

    [DisplayName("Answer to First Secret Question")]
    [Required]
    public string SecretQuestionAnswer1 { get; set; }

    [DisplayName("Second Secret Question")]
    [Required]
    public int SecretQuestion2Id { get; set; }

    public IList<SelectListItem> SecretQuestion2IdList { get; set; }

    [DisplayName("Answer to Second Secret Question")]
    [Required]
    public string SecretQuestionAnswer2 { get; set; }

    [Required]
    public int TrustedDomainId { get; set; }

    public IList<SelectListItem> TrustedDomain { get; set; }

}



@model ExternalUserManagement.Web.Mvc.Controllers.ViewModels.Register.RegisterPageViewModel
@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="accountDetails" class="centre-container">
    @using (Ajax.BeginForm("CreateAccount", "Register",
        new AjaxOptions
        {
            UpdateTargetId = "accountDetails",
            OnBegin = "windowHelper.displayWaitingDialog('Saving Registration Details, please wait...')",
            OnComplete = "windowHelper.close()"
        }))
    {  
        <p class="message information">Register you details, then click submit.</p>

        <div class="row">
            @Html.LabelFor(m => m.FirstName, new { @class = "label" })
            @Html.TextBoxFor(m => m.FirstName, new { @class = "input k-textbox" })
        </div>
        <div class="row">
            @Html.LabelFor(m => m.LastName, new { @class = "label" })
            @Html.TextBoxFor(m => m.LastName, new { @class = "input k-textbox" })

        </div>
        <div class="row">
            @Html.LabelFor(m => m.CompanyEmail, new { @class = "label" })
            @Html.TextBoxFor(m => m.CompanyEmail, new { @class = "input-left k-textbox" })
            @&nbsp;
            @(Html.Kendo().DropDownListFor(m => m.TrustedDomainId)
                      .DataTextField("Text")
                      .DataValueField("Value")
                      .BindTo(Model.TrustedDomain)
                      .OptionLabel(" -- Please Select --")
                      .HtmlAttributes(new { @class = "input-right" })
                      )
        </div>
        <div class="row">
            @Html.LabelFor(m => m.BirthDate, new { @class = "label" })
            @Html.Kendo().DatePickerFor(m => m.BirthDate).HtmlAttributes(new { @class = "input" })
        </div>
        <div class="row">
            @Html.LabelFor(m => m.SecretQuestion1Id, new { @class = "label" })
            @(Html.Kendo().DropDownListFor(m => m.SecretQuestion1Id)
                      .DataTextField("Text")
                      .DataValueField("Value")
                      .BindTo(Model.SecretQuestion1IdList)
                      .OptionLabel(" -- Please Select --")
                      .HtmlAttributes(new { @class = "input" })
                      )
        </div>
        <div class="row">
            @Html.LabelFor(m => m.SecretQuestionAnswer1, new { @class = "label" })
            @Html.TextBoxFor(m => m.SecretQuestionAnswer1, new { @class = "input k-textbox" })
        </div>
        <div class="row">
            @Html.LabelFor(m => m.SecretQuestion2Id, new { @class = "label" })
            @(Html.Kendo().DropDownListFor(m => m.SecretQuestion2Id)
                      .DataTextField("Text")
                      .DataValueField("Value")
                      .BindTo(Model.SecretQuestion2IdList)
                      .OptionLabel(" -- Please Select --")
                      .HtmlAttributes(new { @class = "input" }).AutoBind(true)
                      )
        </div>
        <div class="row">
            @Html.LabelFor(m => m.SecretQuestionAnswer2, new { @class = "label" })
            @Html.TextBoxFor(m => m.SecretQuestionAnswer2, new { @class = "input k-textbox" })
        </div>
        <div class="captcha row">
            @Html.Label("Are you a human?", new { @class = "label" })
            <br />
            @Html.Raw(Html.GenerateCaptcha("captcha", "clean"))
            @Html.ValidationMessage("Invalid Characters")
        </div>
        <div class="row">
            <div class="commands">
                <button class="k-button" type="submit" title="Sumbit">
                    <img src="@Url.Content("~/Content/Images/Icons/disk.png")" alt="" />
                    Sumbit
                </button>
            </div>
        </div>
    }
</div>

请查看以下内容

链接: http://macaalay.com/2014/02/15/enabling-asp-net-mvc-client-validation-for-kendo-ui-components/

相关问题