Last post Jun 20, 2018 01:20 AM by bruce (sqlwork.com)
Jun 19, 2018 04:35 PM|JORGEMAL|LINK
I am new to MVC (coming from Web Forms) and working in my first project.
In web forms, a click event is "associated" to a click method in order to perform a server-side process, a DB operation for example.
After doing some research I have found that, in MVC, a server-side process can be triggered via an AJAX call to run a web service, a RESTful web service for example.
My question is:
What is an optimal way to perform a server-side process in an MVC application? or What is a good practice? or What is the preferred method?
Is running a web service a good approach?
Jun 19, 2018 04:56 PM|some_yahoo|LINK
By default, MVC wants to use Entity Framework to talk to a database, but it can use a REST service too.
You can call stored procedures or functions directly from a SqlCommand, same as winforms if you like.
Jun 19, 2018 06:02 PM|DA924|LINK
You can use Ajax on the client-side to call a WebAPI. You can also call a WebAPI in MVC code in an async manner too. You can also use Ajax to call a MVC controller action method.
Since you're new to MVC, you should keep this in mind, becuase after all, it's called Model View Controller and not VVC Viewbag View Controller, and you don't want to fall down the viewbag rabbit hole.
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft
Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.
A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained
in the model.
In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.
Jun 20, 2018 01:20 AM|bruce (sqlwork.com)|LINK
you are mixing concepts. you use ajax in MVC for same reasons you do with webforms (or when you would do an update panel). webforms had web methods, or you could include wcf or webapi. with MVC your ajax can all action methods or webapi.
in webforms a button click is a postback to the code behind. in mvc a button click is postback to an action method.
in MVC to know if a particular button did the postback, you just need to know how html form posts work. the browser includes the buttons name and value in the post back data. in the controller you just check for it (webform did this behind the scenes and
fired an event). a common practice in MVC is name all the submit buttons the same, and give them different values. then add the name to postback model or as an action parameter and check check the value to know which one.
webforms builds a control tree at init, and updates the tree with postback values in the load data event. at render, it walks the tree and renders the html
mvc maps the postback data to action methods parameters and call the action. the action returns a view model, and the name of a razor template to render the model. the razor temples converts the model to html.