最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Blazor two-way bind not detected when is updated from JS - Stack Overflow

programmeradmin5浏览0评论

I have a simple two-way binded ponent:

<input type="text"  id="myInput" bind="@MyVar" />
...
@functions {
    private string MyVar { get; set; } = "foo";

All runs fine when I write text on input box. But, if input value is set from javascript then blazor is no able to detect the changed value.

document.getElementById('myInput').value='Random Value';

I tried to raise some events on element like 'key pressed' and so but the private var MyVar has no changes on blazor client side.

I would like to send back to blazor some values from client, I guess changing value of a hidden input may be solution, but not working.

I have a simple two-way binded ponent:

<input type="text"  id="myInput" bind="@MyVar" />
...
@functions {
    private string MyVar { get; set; } = "foo";

All runs fine when I write text on input box. But, if input value is set from javascript then blazor is no able to detect the changed value.

document.getElementById('myInput').value='Random Value';

I tried to raise some events on element like 'key pressed' and so but the private var MyVar has no changes on blazor client side.

I would like to send back to blazor some values from client, I guess changing value of a hidden input may be solution, but not working.

Share Improve this question asked Aug 8, 2018 at 14:11 dani herreradani herrera 51.8k10 gold badges130 silver badges186 bronze badges 2
  • I believe it is not intended to work that way, why do you want this? – Flores Commented Aug 8, 2018 at 16:06
  • hi mr @flores, thanks about your ment, see answer to your ment below Isaac's answer. Thanks! – dani herrera Commented Aug 8, 2018 at 20:18
Add a ment  | 

2 Answers 2

Reset to default 4

As Blazor attaches itself to the onchange event you can make this work by triggering the 'onchange'-event on the element after you've set it.

var element = document.getElementById('myInput');
element.value = 'Random Value';
element.dispatchEvent(new Event('change'));  //<--makes Blazor see the change

There is in many cases no need to reinvent everything when you can use existing javascript-libraries.

When you enter a value into the input box an event is raised, and thus Blazor knows of that, and it updates the property ( MyVar ) to which the input box is bound (this is slso how Angular two-way binding works), but when you change the value of the input box from JavaScript, Blazor has no way to know about this occurrence.

But, hey, why would you do such a thing ?

To send data from JavaScript to Blazor, you shoud define a public and static method in Blazor, annotated with the attibute [JSInvokable], and a JavaScript function that calls this method:

[JSInvokable]
public static Task SendMessageAsync(string message)
{
    // Do something with message
}

DotNet.invokeMethodAsync(assemblyName, 'SendMessageAsync', "Hello Blazor");

Read more here: https://blogs.msdn.microsoft./webdev/2018/07/25/blazor-0-5-0-experimental-release-now-available/

发布评论

评论列表(0)

  1. 暂无评论