PartialView和不显眼客户端验证不工作 [英] PartialView and unobtrusive client validation not working

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

问题描述

我目前使用ASP.NET MVC3 RC和我使用的是不显眼的JQuery验证作为的他的博客。它的伟大工程,但是当我把我的表(阿贾克斯)到服务器上,我做了一些服务器端验证并返回同一行(包括在局部视图)如果我的模型状态无效。 2问题与:

I'm currently using ASP.NET MVC3 RC and I'm using the unobtrusive JQuery validations as described by Brad Wilson on his blog. It works great but when I send my form (in Ajax) to the server, I do some server side validations and return the same row (that is included in a partial view) if my model state isn't valid. 2 problems with that :

1:当我做了在我的行动回报PartialView ,所有的不显眼的属性不会呈现。我发现了一个不高雅的方式做到这一点,但是当我这样做,客户端验证是坏了。之后,我从我的行动回来,即使我称之为 jQuery.validator.unobtrusive.parse()我更新的行, $(形式) .valid(),即使它不是这种情况总是返回true。

1st : When I do a return PartialView in my action, all the unobtrusive attributes aren't rendered. I found a "non elegant" way to do it but when I do it, client validations are broken. After I return from my action, even if I call jQuery.validator.unobtrusive.parse() on my updated row, $("form").valid() always return true even if it's not the case.

第二:我希望我的渲染视图将呈现为在服务器上的一个字符串,所以我可以在一个JsonResult(前寄回: myJSonResult.html = RenderPartialToString(partialName模型))。

2nd : I want my rendered view to be render as a string on the server so I can send it back in a JsonResult (ex:myJSonResult.html=RenderPartialToString("partialName",model)).

具有借鉴意义,有我的观点(editInvitation):

Has a reference, there's my view (editInvitation) :

<td>
    <%= Html.HiddenFor(x=>x.ID,new{id="ID"}) %>
    <%= Html.HiddenFor(x=>x.GroupID,new{id="GroupID"})  %>
    <%: Html.TextBoxFor(x => x.Name, new { id = "Name" })%><%:Html.ValidationMessageFor(x=>x.Name) %>
</td>
<td>
    <%: Html.TextBoxFor(x => x.Email, new { id = "Email" })%>  <%:Html.ValidationMessageFor(x=>x.Email) %>
</td>
<td>
    <%: Model.Status.ToFriendlyName()%>
</td>
<td>
  <%= InvitationsViewModel.RenderActions(Model, Html, InvitationsViewModel.CreateRowID(Model.ID))%>
</td>

和我控制器操作:

if (TryUpdateModel(invitation))
{
    validModel = true;
    //Other stuff
}
if (Request.IsAjaxRequest())
{
     //TODO : I return a partial view but I would prefer to return a JSonResult with the rendered view as a string in an Property of my JSon result
     return PartialView(validModel ? "DisplayInvitation" : "EditInvitation", invitation);
}

感谢

推荐答案

我终于做它的工作。这就是:

I finally make it worked. This is how :

HtmlHelper helper = GetHelper();
MvcHtmlString partialView = helper.Partial("myView" , model);
var result = new { success = ModelState.IsValid, html = partialView.ToString() };
return Json(result);

有辅助功能:

protected HtmlHelper GetHelper()
{
    return GetHelper(string.Empty);
}
protected HtmlHelper GetHelper(string formID)
{
    HtmlHelper helper = new HtmlHelper(getViewContext(formID), new ViewPage { ViewData = this.ViewData });
    helper.EnableClientValidation(isClientValidationEnabled());
    helper.EnableUnobtrusiveJavaScript(isUnobtrusiveJavascriptEnabled());
    return helper;
}
private ViewContext getViewContext(string formID)
{
    var vc = new ViewContext(this.ControllerContext, new WebFormView(this.ControllerContext, "~/Views/Home/Index.aspx"), this.ViewData, new TempDataDictionary(), new System.IO.StringWriter());
    vc.UnobtrusiveJavaScriptEnabled = isUnobtrusiveJavascriptEnabled();
    vc.ClientValidationEnabled = isClientValidationEnabled();
    vc.FormContext = new FormContext { FormId = formID };
    return vc;
}

我不知道它是更好的方式来做到这一点,但它为我工作。让我们希望在ASP.NET MVC团队将提供一个更简单的方法来呈现一个视图作为字符串。

I'm not sure it's the better way to do it but it worked for me. Let's hope the ASP.NET MVC team would provide an easier way to render a view as a string.

感谢

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

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