3

I have basic viewmodel on server-side, let it be on C# language and ASP.NET Core server-side, for example:

public class BookViewModel
{
 public string Id { get; set; }
 public string Name { get; set; }
 //other properties removed for brevity
}

Then, I send it to client page.

On client-side, I have rich UI with some Javascript MVVM framework.

These frameworks manipulates with viewmodels as I understand. So, the trouble, is that I don't understand what we really must do with viewmodels on client-side?

I mean, If I want send my viewmodel from client to server via Ajax, I need explicitly define all properties of that viewmodel. So, I need explicitly define all my server-side viewmodels, as Javascript objects in special file, whatever. How can I avoid such routine work?

Laiv
15k2 gold badges34 silver badges71 bronze badges
asked Sep 30, 2016 at 16:11

1 Answer 1

4
  1. Using tooling. Most mature frameworks have mechanisms to produce client code from a server model/wsdl/schema etc. Sometimes these are known as code generators. Typically something is inspected on the server side and then produces code that is native to the framework being used on the client.

Be aware that auto-generated code can sometimes be less then optimal but it can be tweaked as needed.

Here's an example:

C# to Knockout View Model

  1. The client model is sometimes different than the server's model so sometimes there a translation layer between the server model and the client. The items that come down from the server are not used directly, but translated from the message into the client's model.

  2. Code it manually. Yes this is laborious/tedious.

answered Sep 30, 2016 at 17:30
1
  • Simple and great answer! I really didn't think about tooling. Commented Sep 30, 2016 at 19:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.