Validate dynamically added control

后端 未结 2 1142
挽巷
挽巷 2021-01-12 02:15

How can I add the dynamically added control to validation?

@*@Html.EditorFor(model => model.Middlename)*@
2条回答
  •  一向
    一向 (楼主)
    2021-01-12 02:26

    It is technically feasible to add to the MVC client side validation by pushing values into the "window.mvcClientValidationMetadata" array of validators. There's a pretty good article here which shows some of the scripts and techniques involved. This will not help you with server side validation, but as the client won't get this input without javascript you have mitigated this.

    I would normally apply a more standard progressive enhancement pattern to this. Create the middle name input by default, and implement standard MVC validators. Then use jQuery to hide it unless (or until) required. Of course this may not fit with your requirements, but if it does, then I'd argue that's a better methodology.

提交回复
热议问题