简体   繁体   English

MVC的无敲击验证使用dropout.js?

[英]MVC Unobtrusive validation for a dropdown using knockout.js?

I am using knockout.js to populate a dropdown: 我正在使用knockout.js填充下拉列表:

<select data-bind="options: AvailableUsers, optionsText: 'DisplayName', value: SelectedUser, optionsCaption: '-- Select a User --'" data-val="true" data-val-required="You must select a user." id="SelectedUser" name="SelectedUser"></select>
<span class="field-validation-valid" data-valmsg-for="SelectedUser" data-valmsg-replace="true"></span>

and I am registering the validator to the form and having it called on the submithandler (I dont think this is related to the problem since the validation is executing): 并且我正在将验证器注册到表单并在submithandler上调用它(我认为这与问题无关,因为验证正在执行):

$.validator.unobtrusive.parse("#UserProfileCreation");
$("#UserProfileCreation").data("validator").settings.submitHandler = mappedModel.save;

However when trying to submit the page, it always acts like the dropdown has no selected value. 但是,当尝试提交页面时,它总是像下拉菜单没有选定的值那样工作。 Even when I confirm via console that SelectedUser has a value. 即使当我通过控制台确认SelectedUser有一个值时。 I have successfully done the same thing in other pages for textareas like so: 我已经在其他页面成功完成了针对textareas的相同操作,如下所示:

<textarea style="width: 100%; height: 50px; min-height: 30px;" name="GroupReply" id="GroupReply" data-bind="value: GroupReply" data-val="true" data-val-required="You must enter a reply."></textarea><br/><span class="field-validation-valid" data-valmsg-for="GroupReply" data-valmsg-replace="true"></span>

And that works fine. 而且效果很好。 So I am not sure what I am missing for the dropdown, but whether I select an option or not, it keeps acting like it's blank and bringing up the validation error message. 因此,我不确定该下拉菜单缺少什么,但是无论是否选择一个选项,它始终表现为空白,并显示验证错误消息。 What am I missing? 我想念什么?

I figured it out and it was quite simple due to my lack of understanding of how knockout handles dropdown's selected values. 我弄清楚了,它很简单,因为我对敲除如何处理下拉列表的选定值缺乏了解。

My AvailableUsers in the KO View Model consisted of a list of KeyValueModels which were based off a C# MVC class (converted using the KO mapping plugin): 我在KO View模型中的AvailableUsers由基于C#MVC类(使用KO映射插件转换)的KeyValueModels列表组成:

public class KeyValueModel{
   public Guid Id {get; set;}
   public string DisplayName {get; set;}
}

I simply needed to add optionsValue: 'Id' to the data-bind attribute of the dropdown. 我只需optionsValue: 'Id'下拉列表的data-bind属性添加optionsValue: 'Id' However it should be noted that this only works because I am passing a Guid as the SelectedUser property of the MVC View Model in the action parameter. 但是应该注意,这仅行得通,因为我在操作参数中将Guid作为MVC视图模型的SelectedUser属性传递。

There have been times where I wanted to pass an entire javascript object that the dropdown selection represents to the MVC view model, in those cases this solution wouldn't work. 有时候,我想将下拉列表所代表的整个javascript对象传递给MVC视图模型,在这种情况下,这种解决方案将行不通。

Note in console, if you do NOT have the optionsValue: 'Id' you select an option from the dropdown and type mappedModel.SelectedUser() you get: 请注意,在控制台中,如果您没有optionsValue: 'Id' ,则从下拉列表中选择一个选项,然后键入mappedModel.SelectedUser()您将获得:

Object {Id: "adb9ae2d-01c8-468d-96e6-06ec39039e29", DisplayName: "Johnson, John"}

Because knockout can store the whole selected object. 因为敲除可以存储整个选定对象。 HOWEVER, knockout does not set ANY value to the options in the dropdown in the actual HTML markup, they are all null (which is why the validation was failing). 但是,剔除并不会为实际HTML标记的下拉菜单中的选项设置任何值,它们都是空的(这就是验证失败的原因)。

If you do add optionsValue: 'Id' and type mappedModel.SelectedUser() into console, then you would simply get: 如果您确实添加optionsValue: 'Id'并在控制台中键入mappedModel.SelectedUser() ,那么您将简单地得到:

"adb9ae2d-01c8-468d-96e6-06ec39039e29"

Which for my purposes on this page is fine. 出于我在此页面上的目的,这很好。 As mentioned if you wanted to pass an entire object to the MVC action based on that selection, this setup would not work. 如前所述,如果您希望基于该选择将整个对象传递给MVC操作,则此设置将不起作用。 You would probably have to do something like setup a hidden field and set it's value to the SelectedUser().Id and put the validation on that hidden field. 您可能需要执行一些操作,例如设置一个隐藏字段并将其值设置为SelectedUser().Id并将验证放在该隐藏字段上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM