【问题标题】:Client validation in Partial View submiting部分视图提交中的客户端验证
【发布时间】:2016-08-05 04:02:42
【问题描述】:

当部分视图直接加载到_Layout中时,客户端验证没有问题并按字段错误显示字段,但是当调用部分视图时与索引页面的一部分相同:

@Html.Partial("_CreateUser", Model)

_CreateUser PartialView:

    @model ProjInMvc.Models.User

<div id="update_panel"></div>

    @using (Ajax.BeginForm("CreateUser", "Home",null, new AjaxOptions { UpdateTargetId = "update_panel", Url = "/Home/CreateUser", HttpMethod = "Post",InsertionMode =InsertionMode.Replace }, new { @class = "pull-right" }))
    {
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default")" />
        </div>
    </div>
    </div>
}

谁能帮我理解这个问题? Tnx。

【问题讨论】:

  • 在布局或主视图中没有区别。您是否在页面呈现后动态加载部分内容(即使用 ajax)?
  • 是的,_CreateUser局部视图是Ajax形式。
  • 不,我的意思是动态加载表单。如果是这样,您需要重新解析 $.validator
  • 请指导我如何在局部视图中使用“$.validator”?!
  • 参考this answer

标签: asp.net-mvc validation asp.net-mvc-partialview


【解决方案1】:

您可能被打到Html.BeginForm,表格中的表格不正确,请检查

【讨论】:

  • 谢谢,但我这里有一份表格?!
  • 尝试在Ajax.BeginForm 中添加 jquery 验证包并给我反馈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 2012-12-17
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多