简体   繁体   中英

MVC creating multiple objects from single view

Just to make it clear, my question is how do you CREATE multiple objects using a single view. My ViewModel works fine, I can display multiple objects no problem .

Its been a while between .NET coding (last time I was coding in 2.0). I have created an MVC 4 project and successfully created a ViewModel (displaying data on a single view from multiple objects).

However, I cheated. I populated the database directly. I now face the question in reverse, what is the best practice for creating multiple objects from a single view?

In my example, I have a User , who has a userId . The userId is a foreign key in UserDetails .

Just trying to get back into the swing of it and wondering what you guys do?

There are 6 ways to pass multiple object in MVC

  1. ViewModel
  2. Partial View
  3. ViewBag
  4. ViewData
  5. TempData
  6. Tuple

Each one have there own pros and cons.you have to decide base on your issue in hand. For more information you can refer code project article on it: How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC

Let me give advantage and disadvantage of View Model

ViewModel : Advantages

  • ViewModel allows us to render multiple model types in a View as a single model.
  • Great intellisense support and compile time error checking on View page.
  • ViewModel is good for security purpose also as Views have only what they exactly need. Core domain models are not exposed to user.
  • If there is any change in core domain model, you do not need to change anywhere in View code, just you need to modify corresponding ViewModel.

Disadvantages

  • ViewModels add another layer between Models and Views so it increases the complexity a little bit. So for small and demo applications, we can use tuple or other ways to keep the things simple for demo.

Option 1: The best approach is to use strongly typed views with Model or ViewModel.

For Example, you have two classes User and Education , you want to display all education details of user in a view, you can create a custom view model and pass it to view, where you view is strongly typed view model:

public class User
{

public int UserId {get;set;}
public string UserName {get;set}
-------
------
}

public class Education
{
public int UserId {get;set;}
public int DegreeId {get;set;}
public long Marks {get;set;}
---------------
--------------
}

Now create a view Model like this:

public class EducationViewModel
{

public User user {get;set;}
public List<Education> educationList {get;set;}
}

now pass the ViewModel to View and do this in View:

@model AppNameSpace.ViewModels.EducationViewModel

Tip: Create a folder named ViewModels and put all the viewmodel classes in it.

Option2: Option 2 is to user ViewBag and pass multiple object from control to your view, ViewBag is accessible when you set some value in it in controller, you can access in the view of that action, after that it is automatically washed out, and its null if you access again it.

you can use ViewBag like this:

ViewBag.Message = "Using ViewBag";

and read value like this in View:

string Message = ViewBag.Message as string;

Option 3:

Option 3 is to store data in TempData , its once read only, means you set value in it, and when you read it, its automatically removed, TempData internally uses Session Variables.You can use TempData like this:

TempData["Key"] = "value";

now you read it in view:

string val = TempData["Key"] as string;

after reading it, it will be automatically removed, but you can keep it if you need it further like this:

TempData.Keep("Key");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM