I have setup Knockoutjs to dynamically create an editable list of values using the following 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);
With html:
<div class="small-box dynamicData" data-bind="template:{ name: 'requirementType-template', foreach: requirementtypes, afterRender:$root.hookUpValidation }" ></div>
<button data-bind='click: AddType'>Add Type</button>
I have hooked up validation for dynamic data using the code recommended on stackoverflow.
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
However I can't find a way to hook Server Side Model Error to dynamic content.
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?
Thanks