c# - 部分 View 不工作的 MVC3 Ajax 表单验证

标签 c# javascript asp.net-mvc asp.net-mvc-3 razor

我有一个奇怪的问题,部分 View (由 Ajax.ActionLink 呈现)内的不显眼的验证不起作用。

这是局部 View :

@model MyWeb.Models.PersonSkill
@using (Ajax.BeginForm("EditSkill", null, new AjaxOptions { UpdateTargetId = "skills" }, new { id = "EditSkillForm" }))
{
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.YearsOfExperience)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.YearsOfExperience)
    @Html.ValidationMessageFor(model => model.YearsOfExperience)
</div>

<p>
    <input type="submit" value="Save"/>
</p>
}

这是调用者:

@Ajax.ActionLink("Edit", "LoadEditSkill", new { id = item.Id }, new AjaxOptions() { UpdateTargetId = "editSkillDialog" }, new { @class = "editSkill" })

View 渲染良好。它将数据回发到服务器,但它只是不验证。

最佳答案

非常感谢 Joe Tuskan 为我指明了正确的方向。

我通过在调用中添加 OnSuccess 订阅者解决了这个问题:

@Ajax.ActionLink("Edit", "LoadEditSkill", new { id = item.Id }, new AjaxOptions() { UpdateTargetId = "editSkillDialog", OnSuccess = "onSuccess" }, new { @class = "editSkill" })

并像解释的那样添加调用 parse() here :

var onSuccess = function () {
    // enable unobtrusive validation for the contents
    // that was injected into the <div id="result"></div> node
    $.validator.unobtrusive.parse($("#editSkillDialog"));
};

关于c# - 部分 View 不工作的 MVC3 Ajax 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12785086/

相关文章:

c# - 多个 ServiceStack 应用程序与一个 RabbitMQ 服务器

javascript - 将值从 js 发布到 php

java - Spring mvc : how to pass parameters in mvc without method arguments at the controller

c# - Entity Framework - 检查挂起的迁移

c# - 如何在单独的程序集中获取用户 ID

c# - MVC4 局部 View 错误 "Object reference not set to an instance of an object."

c# - HttpClient.GetAsync 永远不会在 Xamarin.Android 上返回

c# - 如何保存到 XML?

C# 扩展方法而不是迭代

javascript - 如何使 HTML 表格高度与其宽度成正比?