ASP.NET Can the ViewModel be modified in the view?

Viewed 110

Maybe it's a bad question, but I am looking for a clarification.

Can the viewmodel be modified in the view?

I am using SignalR in my project and when connecting to the already running server project in my CHAT App, the client added a record when launched to a database table but since the Server project is already running, the server viewmodel collection associated with that same table does not know about the new record. So was thinking to somehow update the server viewmodel (even though sounds wrong) but would like to know anyway.

How can I update the viewmodel for the server to account for this new record added when launching the client project?

So for options was thinking:

1.Trigger a method in the server via SignalR to update viewmodel in the view, if thats even possible.

2.Maybe use signalR in the client to trigger a refresh on the server to reload the updated viewmodel?

3.Use signalR in the client to trigger an Ajax function in the server to reload the table information.

  1. Some other way.
1 Answers

Nope, the model or viewModel cannot be modified in the view. What you could do is remember which input fields the viewmodels are assigned to and just get there values with $("#elementID").val().

Every time you receive a notification from SignalR, get the values from the input fields, then use AJAX to submit data to the controller.

EDIT: Actually, the items you mentioned 1-3 all narrows down to sending an ajax GET or POST request to the controller trigger a server function.

AJAX also has a success or finish callback; .done(function(data){ ... }) wherein data contains the data sent from the server. This way you could update your viewModel values from whatever is returned by the server.

Related