PartialView和不显眼的客户端validation无法正常工作

我目前正在使用ASP.NET MVC3 RC,我正在使用Brad Wilson在他博客上描述的不引人注目的JQueryvalidation。 它工作得很好但是当我将表单(在Ajax中)发送到服务器时,如果模型状态无效,我会进行一些服务器端validation并返回相同的行(包含在局部视图中)。 2个问题:

1st:当我在我的动作中return PartialView时,不呈现所有不显眼的属性。 我发现了一种“非优雅”的方式,但是当我这样做时,客户端validation就会被破坏。 从我的动作返回后,即使我在更新的行上调用jQuery.validator.unobtrusive.parse()$("form").valid()总是返回true,即使不是这样。

第二:我希望我的渲染视图在服务器上呈现为字符串,因此我可以将其发送回JsonResult(例如: myJSonResult.html=RenderPartialToString("partialName",model) )。

有参考,有我的观点(editInvitation):

  x.ID,new{id="ID"}) %> x.GroupID,new{id="GroupID"}) %>  x.Name, new { id = "Name" })%>x.Name) %>    x.Email, new { id = "Email" })%> x.Email) %>        

我的控制器动作:

 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); } 

谢谢

我终于让它奏效了。 这是如何:

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

有辅助function:

 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团队能够提供一种更简单的方法来将视图呈现为字符串。

谢谢