Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to auto-update selected item in dropdown to all the connected clients in SignalR with Knockout.js?
    primarykey
    data
    text
    <p><strong>SOLVED</strong>:</p> <p>Added the new corrected lines of code and commented the old ones.</p> <hr/> <p>For a <strong>real-time</strong> application using <strong>SignalR</strong>, i want changes made by a user to be visible in real-time by all the connected clients. </p> <p>It's <strong>ok</strong> with a simple <strong>textbox</strong>, but when i use a <strong>dropdown list</strong>: when a user select an item, i want the selected item from dropdown to be <strong>automatically set (auto-updated)</strong> to all connected clients.</p> <p><strong>Knockout.js</strong> seems to be the obvious choice, but i think i have a problem on subscribe... or elsewhere?</p> <p>What i have:</p> <p>(<strong>ASP .NET Razor</strong>) <strong>Dropdown</strong>:</p> <pre><code>//@Html.DropDownListFor(model =&gt; model.UserProfile.UserId, (SelectList)ViewBag.DDLUsersId, "Select User", new { @class = "ui-corner-all", @data_bind="value: selectedResponsible_UserId" }) @Html.DropDownListFor(model =&gt; model.UserProfile.UserId, (SelectList)ViewBag.DDLUsersId, "Select User", new { @class = "ui-corner-all", @data_bind="value: Responsible_UserId" }) </code></pre> <p>(<strong>HTML</strong>) which generates:</p> <pre><code>//&lt;select class="ui-corner-all" data-bind="value: selectedResponsible_UserId" id="UserProfile_UserId" name="UserProfile.UserId"&gt;&lt;option value=""&gt;Select User&lt;/option&gt; &lt;select class="ui-corner-all" data-bind="value: Responsible_UserId" id="UserProfile_UserId" name="UserProfile.UserId"&gt;&lt;option value=""&gt;Select User&lt;/option&gt; &lt;option value="1"&gt;test1&lt;/option&gt; &lt;option value="2"&gt;test2&lt;/option&gt; &lt;/select&gt; </code></pre> <p>(<strong>JavaScript</strong>) <strong>ViewModel</strong>:</p> <pre><code>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)); }); } </code></pre> <p>(<strong>JavaScript</strong>) <strong>Function</strong> from <strong>Client-Side</strong> which call the function from <strong>Server-Side</strong> with specified object:</p> <pre><code>this.updateTask = function (task) { if (notify) this.hub.server.s_Update(task); } </code></pre> <p>(<strong>C#</strong>) <strong>Function</strong> from <strong>Server-Side</strong> which modify values in <strong>DB</strong> and call the function from <strong>Client-Side</strong> for all the connected <strong>Clients</strong> with specified object:</p> <pre><code>public bool S_Update(Task updatedTask) { using (var context = new ToDoDbContext()) { var oldTask = context.Tasks.FirstOrDefault(t =&gt; 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; } } } </code></pre> <p>(<strong>JavaScript</strong>) <strong>Function</strong> from <strong>Client-Side</strong> which should <strong>update</strong> the <strong>Interface</strong>:</p> <pre><code>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; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload