Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6820919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:28:58+00:00 2026-05-26T21:28:58+00:00

In ASP.NET MVC there is Model, View and Controller. MODEL represents entities which are

  • 0

In ASP.NET MVC there is Model, View and Controller.

MODEL represents entities which are stored in database and essentially is all the data used in a application (for example, generated by EntityFramework, “DB First” approach).

Not all data from model you want to show in the view (for example, hashs of passwords). So you create VIEW MODEL, each for every strongly-typed-razor-view you have in application.
Like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyProject.ViewModels.SomeController.SomeAction
{
    public class ViewModel
    {
        public ViewModel()
        {
            Entities1 = new List<ViewEntity1>();
            Entities2 = new List<ViewEntity2>();
        }

        public List<ViewEntity1> Entities1 { get; set; }
        public List<ViewEntity2> Entities2 { get; set; }

    }

    public class ViewEntity1
    {
        //some properties from original DB-entity you want to show
    }
    public class ViewEntity2
    {

    }
}

When you create complex client-side interfaces (I do), you use some pattern for javascript on client, MVC or MVVM (I know only these). So, with MVC on client you have another model (Backbone.Model for example), which is third model in application. It is a bit much.

Why don`t we use the same ViewModel model on a client (in backbone.js or another framework)? Is there a way to transfer CS-coded model to JS-coded? Like in MVVM pattern, with knockout.js, when you can do like this:

in SomeAction.cshtml:

<div style="display: none;" id="view_model">@Json.Encode(Model)</div>

after that in Javascript-code

var ViewModel = ko.mapping.fromJSON($("#view_model").get(0).innerHTML);

now you can extend your ViewModel with some actions, event handlers, etc:

ko.utils.extend(ViewModel, {

some_function: function () {
//some code
}

});

So, we are not building the same view model on the client again, we are transferring existing view model from server. At least, data.

But knockout.js is not suitable for me, you can`t build complex UI with it, it is just data-binding. I need something more structural, like backbone.js.

The only way to build ViewModel for backbone.js I can see now is re-writing same ViewModel in JS from server with hands.

Is there any ways to transfer it from server? To reuse the same viewmodel on server view and client view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:28:58+00:00Added an answer on May 26, 2026 at 9:28 pm

    If your ViewModel can be mapped one-to-one to Backbone.js Model, then you can simply serialize your ViewModel to JSON and return it as a result to Backbone.js model request. So in javascript Model, you specify url to ASP.NET MVC action

    Person = Backbone.Model.extend({
    
      url: function() {
        return this.isNew() ? '/Users/Create' : '/Users/' + this.get('id');
      }
    
    });
    

    In ASP.NET MVC you write something like this:

    var data = somedata;
    var viewModel = new ViewModel();
    var serializer = new JavaScriptSerializer();
    viewModel.JsonData = serializer.Serialize(data);
    
    return View("viewname", viewModel);
    

    then use

    <%= Model.JsonData %>
    

    Happy coding!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC and I have a model class which represents a peice
In asp.net mvc there is ViewResult for returning a View and ActionResult for returning
There seem to be multiple means of passing model data from controllers in asp.net
I have an asp.net mvc application with three layers: - data layer with entities
Using ASP.NET MVC when I create my model, then a controller based on the
Using ASP.NET MVC there are situations (such as form submission) that may require a
In ASP.NET MVC there are generic master views ( ViewMaserPage<T> ). But what is
In ASP.NET MVC is there an equivalent of the Html.ActionLink helper for Img tags?
Some texts on ASP.NET MVC state that there are no runat server tags, even
Using ASP.Net MVC 2, is there any way to use the RedirectToAction() method of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.