0

I'm currently developping a website on asp.net MVC 4. I'm a bit confused about the different ways to pass data from the controller to the view.

First of all, if we have a list of objects users, what's the difference between passing this list to the view using:

return View(users);

and

ViewBag.users = users;

My other question is about the first solution. If we use this solution, do we have to use this

@model IEnumerable<mydb.users>

in the View?

Or could we use for instance

@model IEnumerable<mydb.registrations>

I know it would be odd to use a different model in the view than what we've used in the controller, but VS doesn't seem to be bothered.

Thanls a lot for you answers

2 Answers 2

3

You can pass parameters as you want, but the best way is to make your own "view model" for each view.

public class UsersViewModel
{
    public IEnumerable<UserViewModel> Users { get; set; }
    public int UserCount { get; set; }
}

Then pass this view model back to the view:

var viewModel = new UsersViewModel();
// ...
return View(viewModel);

You can use Automapper tool to automatically convert your entities to viewmodels and back. It will look like this:

// in Global.asax.cs on Application_Start

Mapper.CreateMap<User, UserViewModel>();
Mapper.CreateMap<IEnumerable<User>, UsersViewModel>();

// in your action

var viewModel = Mapper.Map<UsersViewModel>(mydb.users);

Your view model will be created automatically, check automapper docs for more info. Good examples on Automapper usage are available in RacoonBlog.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for your answer. I'm currently reading documentation on MVVM. But I don't really get the difference between creating a ViewModel and directly giving my entities ? Is it just to limited the fields that are sent to the view?
It is considered as a good practice, to separate your view and business logic. Automapper acts as a layer that performs all the neccesarry operations to perform conversion entities <-> viewmodels. If you have small application or limited time & budget, I think it's okay to do this any way you want. But when it comes to good practices and keeping your application maintainable and understandable for others, view models are the good way to go.
1

ViewBag is a container. You can pass anything to the View using the ViewBag say it a string or class or whatever. You can use any no of ViewBags to pass to view from controller.

return View(users); here you have the list there and you can pass only one object as model from controller to view.

The reply to the second question you can receive the object Model to View using @model where we use the reference to a Object in particular which is generic. The controller helps in identifying what is being passed to the view. You can use it in further coding using Model in your View. ex: Model.Users

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.