验证只加载AJAX局部视图 [英] Validate only ajax loaded partial view

查看:206
本文介绍了验证只加载AJAX局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些控件的窗体。有它加载的局部视图的表单上的按钮。里面的局部视图,有一个按钮沿两个必填​​字段文本框。而它的点击时,我只需要对文本框这是局部视图中显示错误消息,但不适合在实际表格中的字段。当我点击表单的提交按钮,所有的错误信息必须显示出来。

I have a form with some controls. There is a button on the form which loads a partial view. Inside the partial view, there are two required field textboxes along with a button. And when its clicked, I need to display error messages only for textboxes which are inside the partial view, but not for the fields in the actual form. And when I click form's submit button, all error messages must show up.

局部视图被加载后,我重新初始化验证插件如下图所示。

After partial view is loaded, I am re-initializing the validation plugin as below.

$('#test').removeData("validator");
$.validator.unobtrusive.parse('#test');

我试着用下面的线程,但它不工作描述验证属性。也许它工作正常装载的意见。

I tried using validation attribute described in below thread but its not working. Maybe it works for normally loaded views.

ASP.NET MVC验证组?

不过,我可以单独调用textbox1.valid()和textbox2.valid()验证。但我觉得我缺少做它的标准方式。任何帮助是AP preciated。

However, I can validate individually by calling textbox1.valid() and textbox2.valid(). But I think I am missing standard way of doing it. Any help is appreciated.

推荐答案

您可以通过提交使用局部视图做到这一点Ajax.BeginForm()

you can do this by submitting your partial view using Ajax.BeginForm()

//In Partail View
@model SomeModel

@using(Ajax.BeginForm(SomeActionName,新AjaxOptions {列举HTTPMethod =POST,UpdateTargetId =目标ID}))
     {

@using (Ajax.BeginForm("SomeActionName", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "targetId"})) {

           @Html.EditorFor(mode=>model.FirstText)
           @Html.EditorFor(mode=>model.SecText)
           <input type="submit" value="save">
 }

//In Controller

public ActionResult SomeAction(SomeModel model)
{
    return PartaiulView(model);
}

在这里你可以验证你的部分景观
注:当您提交您的形式使用Ajax.BeginForm您必须指定 UpdateTargetId ,其中你的结果会出现在视图

here you can validate your Partial View NOTE: when you submit you form using Ajax.BeginForm you must specify "UpdateTargetId" where your result will be appear on View.

//In View

<div id="targetId">
   @Html.Partail("PartialView")
</div>

或者如果你想重定向到另一个动作,如果你的模型是有效的,然后修改你的行动

OR if you want to Redirect to another action if your model is valid then modified your action

public ActionResult SomeAction(SomeModel model)
    {
        if(ModelState.IsValid)
        {
            return Json(new {redirect = @Url.Action("SomeAction","SomeController")})
        }
        return PartaiulView(model);
    }

然后partail视图可以调用的的onSuccess Ajax.BeginForm的方法

then in partail view you can invoke OnSuccess method of Ajax.BeginForm

     @using (Ajax.BeginForm("SomeActionName", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "targetId",OnSuccess="success"}))
         {

         }

<script type="text/javascript">

  function success(data)
  {
        if(data.redirect)
        {
            windows.location = data;
        }
  }
</script>

同时检查方法哪一种适合你。

check both way which one is suitable to you.

这篇关于验证只加载AJAX局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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