本文介绍了如何自动更新所选项目的下拉列表中SignalR与Knockout.js所有连接的客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 解决 加code的新修正的线条和注释旧的。对于实时使用应用程序的 SignalR ,我想通过一个用户所做的更改所有连接的客户端是在实时可见。 这是确定用一个简单的文本,但是当我使用的下拉列表:当用户选择一个项目,我想选择从下拉列表项为自动设置(自动更新)以所有连接的客户。 Knockout.js 似乎是显而易见的选择,但我想我有一个问题关于订阅......或其他地方?我有:( ASP .NET剃须刀)下拉 // @ Html.DropDownListFor(型号=&GT; model.UserProfile.UserId,(的SelectList)ViewBag.DDLUsersId,选择用户,新{@class =UI角-all,@ data_bind =值:selectedResponsible_UserId})@ Html.DropDownListFor(型号=&GT; model.UserProfile.UserId,(的SelectList)ViewBag.DDLUsersId,选择用户,新{@class =UI角一切,@ data_bind =值:Responsible_UserId}) ( HTML )产生: //&LT;选择类=UI刀尖所有的数据绑定=值:selectedResponsible_UserIdID =UserProfile_UserIdNAME =UserProfile.UserId&GT;&LT ;期权价值=&GT;选择用户&LT; /选项&GT;&LT;选择类=UI刀尖所有的数据绑定=值:Responsible_UserIdID =UserProfile_UserIdNAME =UserProfile.UserId&GT;&LT;期权价值=&GT;选择用户&LT; /选项&GT;    &LT;期权价值=1&GT;&TEST1 LT; /选项&GT;    &LT;期权价值=2&GT; test2的&LT; /选项&GT;&LT; /选择&GT; (的JavaScript )视图模型 函数taskViewModel(ID,标题,Responsible_UserId,ownerViewModel){    this.taskId = ID;    this.title = ko.observable(职称);    //this.selectedResponsible_UserId = ko.observable(Responsible_UserId);    this.Responsible_UserId = ko.observable(Responsible_UserId);    this.notification =功能(B){通知= B}    VAR自我=这一点;    this.title.subscribe(功能(newValue)以    {        ownerViewModel.updateTask(ko.toJS(个体经营));    });    //this.selectedResponsible_UserId.subscribe(function(newValue)以    this.Responsible_UserId.subscribe(功能(newValue)以    {        ownerViewModel.updateTask(ko.toJS(个体经营));    });} (的JavaScript )的功能从客户端它调用从函数服务器端与指定对象 this.updateTask =功能(任务){    如果(通知)        this.hub.server.s_Update(任务);} ( C#)的功能从服务器端用于修改值的 DB 并调用从功能客户端作为所有连接的客户端与指定的对象: 公共BOOL S_Update(任务updatedTask){    使用(VAR上下文=新ToDoDbContext())    {        VAR oldTask = context.Tasks.FirstOrDefault(T =&GT; t.taskId == updatedTask.taskId);        如果(oldTask == NULL)            返回false;        其他        {            oldTask.title = updatedTask.title;//?在这里,价值'updatedTask.Responsible_UserId是NULL!            oldTask.Responsible_UserId = updatedTask.Responsible_UserId;            context.SaveChanges();            Clients.All.C_TaskUpdated(oldTask);            返回true;        }    }} (的JavaScript )的功能从客户端应的更新在接口: this.hub.client.C_TaskUpdated =功能(T){    VAR任务= ko.utils.arrayFilter(任务(),功能(价值){返回value.taskId == t.taskId;})[0];    通知= FALSE;        task.title(t.title);//!很明显,这里被设置为NULL。        //task.selectedResponsible_UserId(t.Responsible_UserId);        task.Responsible_UserId(t.Responsible_UserId);    通知= TRUE;}; 解决方案 如果我按照这个正确的,你的道具叫做 selectedResponsible_UserId 在客户端和 Responsible_UserId 在服务器上。貌似这个潜在的不匹配是造成您的问题。SOLVED:Added the new corrected lines of code and commented the old ones.For a real-time application using SignalR, i want changes made by a user to be visible in real-time by all the connected clients. It's ok with a simple textbox, but when i use a dropdown list: when a user select an item, i want the selected item from dropdown to be automatically set (auto-updated) to all connected clients.Knockout.js seems to be the obvious choice, but i think i have a problem on subscribe... or elsewhere?What i have:(ASP .NET Razor) Dropdown://@Html.DropDownListFor(model => model.UserProfile.UserId, (SelectList)ViewBag.DDLUsersId, "Select User", new { @class = "ui-corner-all", @data_bind="value: selectedResponsible_UserId" })@Html.DropDownListFor(model => model.UserProfile.UserId, (SelectList)ViewBag.DDLUsersId, "Select User", new { @class = "ui-corner-all", @data_bind="value: Responsible_UserId" })(HTML) which generates://<select class="ui-corner-all" data-bind="value: selectedResponsible_UserId" id="UserProfile_UserId" name="UserProfile.UserId"><option value="">Select User</option><select class="ui-corner-all" data-bind="value: Responsible_UserId" id="UserProfile_UserId" name="UserProfile.UserId"><option value="">Select User</option> <option value="1">test1</option> <option value="2">test2</option></select>(JavaScript) ViewModel:function taskViewModel(id, title, Responsible_UserId, ownerViewModel){ this.taskId = id; this.title = ko.observable(title); //this.selectedResponsible_UserId = ko.observable(Responsible_UserId); this.Responsible_UserId = ko.observable(Responsible_UserId); this.notification = function (b) { notify = b } var self = this; this.title.subscribe(function (newValue) { ownerViewModel.updateTask(ko.toJS(self)); }); //this.selectedResponsible_UserId.subscribe(function (newValue) this.Responsible_UserId.subscribe(function (newValue) { ownerViewModel.updateTask(ko.toJS(self)); });}(JavaScript) Function from Client-Side which call the function from Server-Side with specified object:this.updateTask = function (task){ if (notify) this.hub.server.s_Update(task);}(C#) Function from Server-Side which modify values in DB and call the function from Client-Side for all the connected Clients with specified object:public bool S_Update(Task updatedTask){ using (var context = new ToDoDbContext()) { var oldTask = context.Tasks.FirstOrDefault(t => t.taskId == updatedTask.taskId); if (oldTask == null) return false; else { oldTask.title = updatedTask.title;//??? Here, value 'updatedTask.Responsible_UserId' was NULL !!! oldTask.Responsible_UserId = updatedTask.Responsible_UserId; context.SaveChanges(); Clients.All.C_TaskUpdated(oldTask); return true; } }}(JavaScript) Function from Client-Side which should update the Interface:this.hub.client.C_TaskUpdated = function (t){ var task = ko.utils.arrayFilter(tasks(), function (value) { return value.taskId == t.taskId; })[0]; notify = false; task.title(t.title); //!! obvious, here was set to NULL. //task.selectedResponsible_UserId(t.Responsible_UserId); task.Responsible_UserId(t.Responsible_UserId); notify = true;}; 解决方案 If I follow this correctly, your prop is called selectedResponsible_UserId on the client-side and Responsible_UserId on the server. Looks like this potential mismatch is causing your issue. 这篇关于如何自动更新所选项目的下拉列表中SignalR与Knockout.js所有连接的客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 19:36