使用jQuery的ASP.NET MVC 2加载局部视图 - 无客户端验证 [英] ASP.NET MVC 2 loading partial view using jQuery - no client side validation

查看:70
本文介绍了使用jQuery的ASP.NET MVC 2加载局部视图 - 无客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jQuery.load()渲染的局部视图。这部分看起来是这样的:

I am using jQuery.load() to render a partial view. This part looks like this:

$('#sizeAddHolder').load(
                '/MyController/MyAction', function () { ... });

在code在我控制器操作如下:

The code for actions in my controller is the following:

    public ActionResult MyAction(byte id)
    {
        var model = new MyModel
        {
            ObjectProp1 = "Some text"
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult MyAction(byte id, FormCollection form)
    {
        // TODO: DB insert logic goes here

        var result = ...;

        return Json(result);
    }

我回来,看起来像这样的局部视图:

I am returning a partial view that looks something like this:

<% using (Html.BeginForm("MyAction", "MyController")) {%>
    <%= Html.ValidationSummary(true) %>

    <h3>Create my object</h3>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.ObjectProp1) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.Size.ObjectProp1) %>
            <%= Html.ValidationMessageFor(model => model.ObjectProp1) %>
        </div>

        div class="editor-label">
            <%= Html.LabelFor(model => model.ObjectProp2) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.ObjectProp2) %>
            <%= Html.ValidationMessageFor(model => model.ObjectProp2) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

客户端验证不会在这种情况下工作。更重要的是,它包含验证消息也没有包括在所返回视图的脚本。在我的模型类这两个属性有要求 StringLength 属性。
是否有已加载像这样的任何方式来触发客户端验证在视图中?

Client side validation does not work in this case. What is more the script that contains validation messages also isn't included in the view that's returned. Both properties in my model class have Required and StringLength attributes. Is there any way to trigger client side validation in a view which has been loaded like this?

推荐答案

这是适用的:<一href=\"http://weblogs.asp.net/imranbaloch/archive/2010/07/11/asp-net-mvc-client-side-validation-with-dynamic-contents.aspx\"相对=nofollow> ASP净MVC-客户端验证与 - 动态内容

这篇关于使用jQuery的ASP.NET MVC 2加载局部视图 - 无客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆