Tuesday, 22 October 2013

View without Controller Action in MVC


In this quick post you will learn how a view can be rendered without its native Controller
Action method.

Why we need this?

Let’s look at the image.


In above image, you can see for each view we have matching controller action. Each of
these actions contains a single line of code. In fact, each of these actions contains
exactly the same line of code. And this is completely a needless work. Even imagine
what you will do when you have hundreds or thousands of views. Will you create
hundreds or thousands of controller actions? Off course not, then how can we fix it?

In MVC Framework, controller class includes a method HandleUnknownAction() that
executes whenever we attempt to invoke an action (or when we request a view which
has no matching action method) on a controller that does not exist.


Now we are taking the advantage of the HandleUnknownAction() method to render views
even when a corresponding controller method does not exist.

In above image you can see we don’t have Post5.cshtml, so when I tried to access the
Post5.cshtml view, it pops following error.


Exception Generated



To fix this issue, we can use simple try-catch block and redirect the user on

PageNotFound view, here’s how.



I Hope this will Help you to Learn.
Continue Reading →

What is Model and ViewModel in MVC Pattern?

Model and ViewModel are two things we always hear in MVC. And in this post I am
going to show you the differences between them.

Let’s begin with its common definition.

What is Model or Domain Model?

Actually, the word 'model' has hundreds of meaning in software development, but
here we gone talk about 'model' in MVC design pattern. I would define model as an
object that we use to send information to the database, to perform business
calculations, to render view. In other word, 'model' represent the domain of
the application helps us in saving, creating, updating and deleting records. Usually
we put all our model classes in Model folder.

What is ViewModel?

ViewModel in MVC design pattern is very similar to 'model'. The major difference
between 'Model' and ‘ViewModel’ is that we use ViewModel only in rendering views.
We put all our ViewModel classes in ‘ViewModels’ named folder, we create this folder.

Understand it with example

Let's assume, we want to implement a view page that will have three textboxes for
Username, Password and Re-enter Password. To achieve this we could design a
'Model' as given below:

    public class Login
    {
        public String Username { getset; }
        public String Password { getset; }
        public String RePassword { getset; }
    }

For sake of view this model works fine. But this is actually a wrong approach because
we are trying to overcrowd the database. I can't see any use of 'RePassword'
property in database.

Now, if we take the advantage of ViewModel, we can safeguard the database from
overcrowding with fields. Here’s how, design following ‘Model’ which will be our
Domain Model:-

    //this will represent domain of the application
    public class Login
    {
        public String Username { getset; }
        public String Password { getset; }
    }

And then following 'ViewModel':-

    //this will help in rendering great views
    public class LoginViewModel
    {
        public String Username { getset; }
        public String Password { getset; }
        public String RePassword { getset; }
    }

Now, when adding view, pick the ViewModel class to get the strongly-typed benefits.


Now the question is how do we transform the Model or Domain Model from the
‘ViewModel’? Let’s learn it.

Transforming Model from ViewModel

There are various ways to do this. In the form POST action we can create a new
object of type Login model and then assign the properties one by one and leave the
unwanted properties.

[HttpPost]
public ActionResult Login(LoginViewModel viewModel)
{
    //validate the ViewModel

    //transforming Model (Domain Model) which is Login from ViewModel which is
        LoginViewModel
    var login = new Login()
    {
        Username = viewModel.Username,
        Password = viewModel.Password
    };

    //save the login var

    return View();
}

In above code, after validating the ViewModel I’m transforming the Model or Domain
Model. So, by using this way you can stop overcrowding database with
unwanted fields.

I Hope this will help you to understand the use of Model and ViewModel.




Continue Reading →

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives