MVC validation lost in Knockoutjs post

有些话、适合烂在心里 提交于 2019-12-04 11:43:27

What I question. I was exploring this topic for the last 2 weeks. (Look up my questions from last few days if you want to). I came to conclusion that data annotations and fluent validation doesn't work on client side that is using knockout unfortunately. Hence you have to introduce another layer of validation.

Good news is that its well tested and as standard as knockout is.

You have mainly three options:

I was expecting community to tend to suggest using jQuery one, but apparently mainstream validation for knockout is the first choice there. It behaves very similarly to data annotations, but is obviously done on knockout view model fields.

Hope this helps (don't shoot the messenger :) )

Im looking into a similar situation at the moment, doing a hybrid web api / MVC project. Where only the details pages recieve any data from the controller. Rest handled by knockout. Options i can see sofar(some tested, some qualified guess):

  1. Duplicate your validationlogic through the ViewModel and the Knockout Clientside ViewModel(using preferrably Knockout-Validation as suggested above). Instinctively feels funky to me.
  2. Set up your form as a regular mvc strongly typed form using jquery unobtrusive validation.

1,2: Handle events(when form is deemed valid) using knockout / jquery. If async post contains errors return and present them (some css / less / sass frameworks like bootstrap have ready alertbox classes for example to put the error in).

  1. Check out MVC Control Toolkits clientblock. I plan to test this option myself, looks promising.
  2. Try KnockoutMvc, that also seems to be able to help with this.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!