Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I made some progress but I am not quite happy.</p> <p><strong>Problem #1:</strong> The client validation metadata is not generated unless you have a Html.BeginForm() in your partial. Which in my case is false because I do not update the entire form, I update portions of it.</p> <p><strong>Solution for Problem #1:</strong> Add a form in the partial view, let MVC generate the client validation MetaData and remove the form tags with a action filter. Let's call this <strong>Hack #1</strong>.</p> <pre><code>public class RemoveFormFilterAttribute : ActionFilterAttribute { private static readonly MethodInfo SwitchWriterMethod = typeof(HttpResponse).GetMethod("SwitchWriter", BindingFlags.Instance | BindingFlags.NonPublic); private TextWriter _OriginalWriter; public override void OnActionExecuting(ActionExecutingContext filterContext) { _OriginalWriter = (TextWriter)SwitchWriterMethod.Invoke(HttpContext.Current.Response, new object[] {new HtmlTextWriter(new StringWriter())}); } public override void OnResultExecuted(ResultExecutedContext filterContext) { if (_OriginalWriter != null) { HtmlTextWriter lTextWriter =(HtmlTextWriter) SwitchWriterMethod.Invoke(HttpContext.Current.Response, new object[] {_OriginalWriter}); string lOriginalHTML = lTextWriter.InnerWriter.ToString(); string lNewHTML = RemoveFormTags(lOriginalHTML); filterContext.HttpContext.Response.Write(lNewHTML); } } </code></pre> <p><strong>Problem #2:</strong> The initial client validation metadata for the page is gone by the time I have the metaData for the new content.</p> <p><strong>Solution for Problem #2:</strong> Store the initial metadata (hard copy) and update it with the new fieds, than call the methods you mentioned to let MVC know new stuff arrived. Let's call this <strong>Hack #2.</strong></p> <pre><code>&lt;script type="text/javascript"&gt; var pageMvcClientValidationMetadata; $(document).ready(function() { $("input[name='PaymentTypeName']").change(PaymentTypeChanged); //create a back-up of the ValidationMetadata pageMvcClientValidationMetadata = JSON.parse(JSON.stringify(window.mvcClientValidationMetadata)); }); function PaymentTypeChanged() { var selectedPaymentType = $("input[name='PaymentTypeName']:checked").val(); $.ajax( { url: 'PersonalData/GetPaymentTypeHtml?&amp;paymentType=' + selectedPaymentType, type: "GET", cache: false, success: GetPaymentTypeHtml_Success }); } function GetPaymentTypeHtml_Success(result) { $('#divPaymentTypeDetails').html(result); UpdateValidationMetaData(); } function UpdateValidationMetaData() { //update the ValidationMetadata for (i = 0; i &lt; window.mvcClientValidationMetadata[0].Fields.length; i++) { pageMvcClientValidationMetadata[0].Fields.push(window.mvcClientValidationMetadata[0].Fields[i]); } //restore the ValidationMetadata window.mvcClientValidationMetadata = JSON.parse(JSON.stringify(pageMvcClientValidationMetadata)); //Notify the Validation Framework that new Metadata exists Sys.Application.remove_load(arguments.callee); Sys.Mvc.FormContext._Application_Load(); } </code></pre> <p></p> <p>Now. Any improvements would be appreciated.</p> <p><strong>Hack #1:</strong> How can I generate the client validation metadata without having an actual form ?</p> <p><strong>HAck #2:</strong> How can I appent to the page validation metadata ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload