Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get ModelState.AddModel Error to bind to knockout dynamically created validation spans?
    primarykey
    data
    text
    <p>I have setup Knockoutjs to dynamically create an editable list of values using the following code:</p> <pre><code> var requirementModel = function() { var self = this; self.requirementtypes = ko.observableArray(@Html.Interpret(Model.requirementtypes)); self.requirementid = ko.observable(@Html.Interpret(Model.requirementid)); self.AddRequirementType = function() { self.requirementtypes.push({ requirementtypeid: null, number: "", requirementid: 0 }); }; self.RemoveType = function(Type) { self.requirementtypes.remove(Type); }; self.hookUpValidation = function() { $.validator.unobtrusive.parseDynamicContent('.dynamicData'); }; }; var viewModel = new requirementModel(); ko.applyBindings(viewModel); </code></pre> <p>With html:</p> <pre><code> &lt;div class="small-box dynamicData" data-bind="template:{ name: 'requirementType-template', foreach: requirementtypes, afterRender:$root.hookUpValidation }" &gt;&lt;/div&gt; &lt;button data-bind='click: AddType'&gt;Add Type&lt;/button&gt; </code></pre> <p>I have hooked up validation for dynamic data using the code recommended on <a href="https://stackoverflow.com/questions/4406291/jquery-validate-unobtrusive-not-working-with-dynamic-injected-elements">stackoverflow</a>.</p> <p>When I post back to the server (I'm not using JSON just form post) I can do more complex validation and if something fails I can use ModelState.AddModelError("input field name", "I pity the fool that broke this"); This works perfectly with either strongly type or @Html.ValidationMessage("input field name") for non dynamic fields</p> <p>However I can't find a way to hook Server Side Model Error to dynamic content.</p> <p>I have the span tags that work with the client side and they work perfectly. However they aren't getting hooked into data returned after serverside validation fails and return page. Any idea how to acheive this?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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