Server Side and Client Side MVC [closed] - asp.net-mvc

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have started learning ASP.Net MVC, I have few questions with regards to MVC framework [Client Side or Server Side]. You might find these questions stupid but I am really confused and would like to clear my doubts.
Here it is....
ASP.Net MVC, is it server side or client side framework?
Let's say my controller methods getting data from the SQL Server, I convert it into JSON and updating the MVC view, in this case
A. Is it called as Client side data binding or Server side binding?
B. While retrieving the data from database using controller method, do i get entire HTML along with data in the response or just the data that I have requested for?
In ASP.Net, lets say I am inserting data in database on button click event and If I am not wrong, in this case it will submit the entire page to the server, how button click event works in ASP.Net MVC?
I have also read that we can use Angular.JS or knockout.JS with ASP.Net MVC, what purpose Angular.JS or knockout.JS would solve if we use it with ASP.Net MVC?
Regards

ASP.Net MVC, is it server side or client side framework?
> MVC - is Model View Controller - its All-in-One
Let's say my controller methods getting data from the SQL Server, I
convert it into JSON and updating the MVC view, in this case
Is it called as Client side data binding or Server side binding?
> Server side is ALL what you do with C# (In class library/controllers etc)
While retrieving the data from database using controller method, do i
get entire HTML along with data in the response or just the data that
I have requested for?
>If you will use knockout / angular - then you need just get data from server, and data-bind will happen automatically
In ASP.Net, lets say I am inserting data in database on button click
event and If I am not wrong, in this case it will submit the entire
page to the server, how button click event works in ASP.Net MVC?
>Or with submit form (to action url) - or with client framework (knockout/angular) - you just send json
I have also read that we can use Angular.JS or knockout.JS with
ASP.Net MVC, what purpose Angular.JS or knockout.JS would solve if we
use it with ASP.Net MVC?
>Angular more rich framework. In knockout we have only data-bind with html. It depends on what you need for your project.

ASP.Net MVC is a server side framework. (An alternative is ASP.Net WebForms: your events are posts to the same page, abstracted to seem like desktop application events.)
If you want, you can output data as JSON or XML and use this with a client side JavaScript framework to update your views.
You will not have button clicks in ASP.Net MVC. All data is sent to the server as parameters in the URL, GET or POST variables (in truth, WebForms also does this). You can grab the values directly from the Request or use the bindings from MVC to get the values as action parameters.
These frameworks are used to build the views in the client side. To use them, you will have to, mostly, output JSON from your controllers.

MVC is a design pattern that you can apply "anywhere" - e.g. server or client side. In ASP.NET MVC, the MVC pattern (model, view, controller) is first and foremost applied server-side, e.g. the code you write executes on the server. (In your view, you can if you like also utilize some MV* pattern, using your own code or a library like Knockout or Angular. This (JavaScript) is then executed on the client, hence client-side.)
Sometimes a piece of code or software that communicates with some other service or similar is labeled a "client", but that is a different context.
Your binding example would execute on the server, if we're talking about ASP.NET MVC Controller code. (In general, there is no need from the framework point-of-view to do any explicit JSON conversion, but that's a different question.)
ASP.NET MVC features something called "model binding" (google it). It's there out-of-the-box, but you can control it with your own implementations as well. Thanks to this feature, you normally deal with strongly typed data inside your Controller.
ASP.NET MVC and Web Forms are fundamentally different. Explaining exactly how, and how to make buttons in ASP.NET MVC that send data is out of scope, but generally we're talking simple POSTed data, e.g. from an HTML form or whatever.

Related

Knockout js over mvc [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have developed applications using .net MVC with razor view engine.
Recently I was going through knockoutjs being used for MVC applications.
From a 10000 feet bird's eye view , I see that both the approaches have the similar two way binding mechanism .
In the first approach razor syntax for binding the model and in the second we use the knockout data-bind attributes.
My understanding is , why is it that many people are nowadays preferring Knockout and all the buzz recently . Is there any major advantage of replacing razor syntax over knockout library?
They have nothing to do with each other.
ASP.NET MVC
As its name says, it uses MVC (model view controller) technology. That means that you have a model (all the business logic), that can be used to create view models to render the views (by using the razor template). The browser can post data to the controller, and the controller will usually answer by creating a new view model to render a new view, and send it as response to the browser.
So this technology basically uses the browser to send GET or POST requests to the server, and the server answers with rendered views. It can do it directly, or using AJAX. (ASP.NET MVC is much more versatile and can do much more than rendering views, but this is the basic idea).
So ASP.NET MVC involves the server in all operations: it must instance and use a controller, execute an action and send its result as response to the browser.
Knockout
This technology is completely different, in several regards:
it uses MVVM, which consist in a double-way binding between a view (HTML) and a model (JavaScript object). A change in any of the ends is applied automatically on the other end.
it happens on the client side, without using or depending on server resources (as far as it concerns Knockout)
it usually doesn't involve heavy business logic, but simple view models that can show and get data from the user.
This is all what Knockout can do for you. If you need to involve the server, and "heavy" business logic, you need to communicate with the server by sending and receiving objects, which you usually do in JSON format by exposing Web API services. (These services are usually implemented with ASP.NET Web API, but can also be implemented with ASP.NET MVC actions with JSON results, which it's a worse option).
Conclusions
So ASP.NET MVC involves the continuous use of controllers and communication between browser and server, while Knockout is a pure client side technology that doesn't need the server at all. Naturally, to make a Knockout application useful you usually communicate it with the server.
OTOH, Knockout allows to create a Single Page Application, which is a JavaScript application that can run by itself, without loading new full pages from the server, and which usually communicates with services, in the form of Web APIs. One advantage of this is that the application can work even without a server on the other side, and is much more responsive, because it doesn't depend on comminucating with a server to refresh the view.

Good structure for ASP.NET MVC 4 / MVVM / Web API? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a .NET MVC 4 project and just started Kendo UI MVVM framework. MVC uses ViewModels to send data from the controller to the view. I manage all my client side objects via the MVVM framework and use JSON to serialize them and send them back to my controller.
Currently I use the MVC ViewModel to return the data that will be static on my page and use jquery calls to fetch any dynamic data needed on my page. I find it confusing to have 2 methods to retrieve data. (And if I find it confusing, imagine the next developer that will have to work in my code)
I find it a bit useless to send the data to the View via ViewModels when I can easily have a structure where I query the controller (via Web API) on demand in my javascript code and save it into my MVVM view model directly.
I see a lot of advantages to using the MVVM framework in my UI, it makes control binding so much easier.
My question:
What is the best way to get the data from the controller to the MVVM ViewModel?
I feel that using the MVC ViewModel is duplicating work since I could query my Web API via ajax requests instead rather than translating my MVC ViewModel into my MVVM JS ViewModel.
Would it be a good approach to never return a MVC viewModel to my UI and always use client side Web API calls to retrieve all data?
Thanks,
Nicolas
Would it be a good approach to never return a MVC viewModel to my UI
and always use client side Web API calls to retrieve all data?
I would say it depends on your use case.
You could certainly return data that needs to be rendered the moment the page is displayed in the MVC Model. Remember that the MVC Model is rendered into the resulting HTML page on the server. This means that the values can be injected right into the HTML before it is returned to the client.
Also keep in mind that it could take overall longer to render your page completely if you have to hit the server multiple times; once to fetch the HTML then once for each Kendo widget to fetch data async.
That said, I usually end up doing what you said... Just render an MVC view with no Model, then have the Kendo UI widgets fetch their data after page load.
But really, it sort of depends on your data. If you have an MVC Model that contains:
public string Title { get; set; }
And in Razor you had:
<h1>#Title</h1>
Then that isn't really a case where I would want to re-fetch that data async. In those cases I usually do something sort of hacky, and put the value into the page:
<script type="text/javascript">
window.viewData = window.viewData || {};
window.viewData.Title = "#Title"
</script>
<h1 data-bind="text: Title"></h1>
<script src="viewmodel.js"></script>
Then in the viewmodel.js file (which I make a separate file from the returned HTML so that it can be cached by the browser)
(function (viewData) {
var viewModel = kendo.observable({
Title: viewData.Title
});
kendo.bind($("body"), viewModel);
})(window.viewData);
This is just my own approach though. It isn't necessarily the best for all situations. It all just depends what data you are pulling from where and how much.
From my understanding of the MVC architecture, View Model in MVVM and ViewModel in MVC are completely different from each other. What i can make out from your post is that you are using a framework with the help of which you are binding the data to the view. Can you elaborate on the framework or provide us with the code so that it can help others/me to get your question better.

ASP.Net MVC verses WebAPI loading of initial Angular model

I have finished a few MVC partial views which load their data using calls to a webapi Get method, to preload the data used by the angular controller.
This method works but it feels more logical to do this via the initial asp.net-MVC Partial view load via #Model for example. Rather than waiting for the page to load and angular to call the get method of my webservice i could have prepopulated the Model, but im not sure how this would pass the data to Angular using this method.
I have had the same issue (if one call this an issue) and ended up doing binding the model to the partial view at the server side. The main rational for the decision was that the model was already available at the time at the server side and I was not building a Single Page Application.
Had I been developing a SPA, I would store the partials as templates at the client side, then grab the model via WebAPI and do the binding
If you use AngularJS,then it's not need ASP.NET MVC. Just use web api for get data.I written a demo site for AngularJS+ASP.NET WEB API,hope to help you,this is the source code.
Does your web page have a lot of heavy client-side interaction or are you simply using Angular to initialize the data for your page on load?
If there's a lot of client-side interaction, you will probably want to keep using Angular. If not, you might want to go back to using MVC since your use case doesn't really require Angular.

wcf and knockout.js combine

I got my datalayer, business layer ready. Now i want to to implement service layer.
I do not want to implement this layer in wcf ria services. Is there any other way to implement this layer in such a way using wcf, so that I get my model through wcf using js.
For example I have my domain 'Person'. (In domain project). Then in my 'PersonRespository' has
InsertPerson, GetPerson etc. to get and store the 'Person' in database.
Now I want to use asp.net mvc to show the person detais.
So next two layer will be Presentation Layer and service layer and manipulate data on client side using knockout.js and I am stuck on following issues.
Where will be mine Presentation layer will live. I am using asp.net mvc so It should be in model folder of mvc application, Is it wise to copy the same code class (Person) to model folder as well from domain model. Event when they are same.
How I will be able to get 'Person' model class in javascript and able to update it from javascript to database as well.
Is my architecture style is of enterprise level or i am missing something.
Any point to tutorial will be helpful.
If you have any further questions please let me know.
Thanks,
Daljit
Question 1:
No you should not be repeating your code. There is talk about this in the DRY (http://en.wikipedia.org/wiki/Don%27t_repeat_yourself) principals of development.
Question 2:
It is recommended that you serialize your model using a json serializer and send it to your UI. It will be updated etc, and then sent back to the services. Google MVVM pattern in javascript to see how this is done. KnockoutJs is a great start in achieving what you want. Its probably best to check out some examples done in knockoutjs to see what is going on. There are also many examples in MVVM for WPF that might help understanding the pattern at a higher level. I would recommend seeing codeproject.com for indepth MVVM examples.
As far as your layers go, you have many options, but a generic recommendation would be:
1) Presentation must be triggered through MVVM bindings, ie if the binding updates, the UI will then update itself.
2) the asp.net side of things should only update the models when sending updates via ajax to the UI. (not everything needs to be sent via ajax, im not saying that. When it does, it shouldn't also send extra html or js to put in the page).
3) Your models should really come from asp.net to the html page. (this will make things easier later, as the page will only be updated via asp.net models and you won't get items coming from multiple domains, which ends up being a nuisance.
4) Your asp.net site should provide a wrapper for your WCF service, and can foward calls to WCF.
OR
If you didn't want to wrap WCF with asp.net and needed your UI to communicate directly via ajax to WCF (should be a rarer usecase like doing an igoogle like page with widgets, or maybe mobile development with no asp.net interaction, ie full js app) Then you can investigate CORS as an option to go from JS to WCF and JS to asp.net (This is of a hard difficulty, easy to program, hard to get working for WCF as there is very very low documentation on it for in my case non IIS hosted WCF). See this page for information: http://enable-cors.org/

Using Sencha's ExtJS MVC with ASP.NET MVC

I wanted to ask if anyone has tried using combination of Sencha's ExtJS 4 (using MVC approach) with ASP.NET MVC (using view models)?
I have existing ASP.NET MVC 3 app that uses view models and my question is how would this "fit" into Sencha's MVC approach...Would ASP.NET MVC "view model" become ExtJS "model" and then I would define yet another "view model" for ExtJS....Seems a lot of "translating"...
What would be the best approach?
And yes, I am aware of projects that integrate ASP.NET MVC with ExtJS using Ext.Direct, but my question is strictly relating to MVC paradigm on "both" sides (ASP.NET and Sencha ExtJS)
Thanks
Z...
Our approach currently is a what could be described as MVCCM or MVC-CM.
In ExtJs you have the view as panels and boxes etc, a store with a model makes a model and you need some logic to make these components work together which would be the controller.
This ExtJs frontend is situated in a MVC3 project and exposes controller methods that typically returns Json data which it gets from the model back end which is typically made up of entities.
There is no programmatic link between the entities on the server side and the models defined in the stores client side. One could generated the stores from entities, but we have not looked into this yet.
The view in the Microsoft MVC3 framework is just a page that returns some div tags which ExtJS can render stuff into.
While I've not done this with ExtJS, I really don't think there's any conflict. I'm assuming a lot here, I know, but if ExtJS works with JSON and you've got ASP.NET MVC actions that emit JSON, it's really more of a philosophic difference than a technical one.
One difference from a normal MVC app would be that your ASP.NET MVC app might not have any views, since the views would be handled entirely by ExtJS.
From the server side, ASP.NET MVC really doesn't care - it's getting a request that gets mapped to a controller and action, processing the request and returning some result. Whether that result is HTML, JSON, XML or whatever, ASP.NET doesn't care at all.

Resources