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.

public ActionResult Login(LoginViewModel viewModel)
    //validate the ViewModel

    //transforming Model (Domain Model) which is Login from ViewModel which is
    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 →


ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)