不显眼的验证不Ajax.BeginForm工作 [英] Unobtrusive validation doesn't work with Ajax.BeginForm

查看:118
本文介绍了不显眼的验证不Ajax.BeginForm工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有型号1,我把 Ajax.BeginForm()在此查看我有PartialView与模型2这里我把 Ajax.BeginForm查看()。因此,只有在第一种形式的工作不显眼的审定。为什么只有在第一种形式的工作验证?

I have View with Model1 where I put Ajax.BeginForm() and in this View i have PartialView with Model2 where i put Ajax.BeginForm(). So only in first form working unobtrusive validation. Why only in first form working validation?

第一个视图

@model Model1

@using (Ajax.BeginForm("Action1","Controller",null,new AjaxOption(){ onSuccess = "alert('=)')"},null)
{

   <intput type="submit" value="Save" />
}


Model2 model2 = new Model2();
@Html.EditorFor(m=>model2)

*的在Model2的观点,我有。 *

@model Model2 
@using (Ajax.BeginForm("AddStreet","Controller",new AjaxOption(){onSuccess = "alert('=)'")},option,null)
{

        @Html.LabelFor(m => Model.Name):
        @Html.TextBoxFor(m => Model.Name)
        @Html.ValidationMessageFor(m => Model.Name)

       <intput type="submit" value="Save" />
}

感谢@Darin季米特洛夫的答案。
我有新的问题。当我从Form2的行动中发送数据..这样
为什么说任何我不能把模式2 ...一个只能取FormColection ...?

Thanks @Darin Dimitrov for answer. I have new problem. When i sending data from Form2 in Action.. like this i can't take Mode2... A can take only FormColection ... Why can any say?

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddStreet(Model2 form){

    return null;
}

提前感谢!

推荐答案

这是因为第二种观点装有AJAX在后一阶段,你需要调用 $。validator.unobtrusive.parse(。 ..)
 紧接其内容之后被注入到DOM以使不显眼的验证。看<一个href=\"http://weblogs.asp.net/imranbaloch/archive/2011/03/05/unobtrusive-client-side-validation-with-dynamic-contents-in-asp-net-mvc.aspx\">following博客文章了解更多详情。

That's because the second view is loaded with AJAX at a later stage and you need to call $.validator.unobtrusive.parse(...) immediately after its contents is injected into the DOM in order to enable unobtrusive validation. Look at the following blog post for more details.

所以你的情况,而不是的警报的第一AJAX调用的的onSuccess 回调,订阅javascript函数,将调用此方法:

So in your case, instead of alerting in the OnSuccess callback of the first AJAX call, subscribe to a javascript function which will invoke this method:

@using (Ajax.BeginForm(
    "Action1",
    "Controller",
    null,
    new AjaxOptions { 
        OnSuccess = "onSuccess",
        UpdateTargetId = "result"
    },
    null)
)
{
    <input type="submit" value="Save" />
}

,然后在你的JavaScript文件:

and then in your javascript file:

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

这篇关于不显眼的验证不Ajax.BeginForm工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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