Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, MVC View View is a user interface. View displays data from the model to the user and also enables them to modify the data. ASP.NET MVC views are stored in Views folder. Learn More, MVC Model The model represents the data, and does nothing else. The model does NOT depend on the controller or the view. Learn More,

send multiple models or parameters from controller to view?

Description:if I need to send multiple models and some parameters to my view what will be the best approach ? one way I know is to use viewbag variable but someone told me that its not a very good approach because it does not provide the intellisense.
So a bit confused which approach can fulfil the requirement

Posted by: | Posted on: Oct 12, 2018

1 answers

Replies

4

First of all you need to understand the purpose of ViewBag variables, ViewBag transfers data from the controller to the view, ideally temporary data which in not included in a model(i.e page title or any other data which can be layout level or page level data).
Now if in your case you still want to stick with the current approach then having the intellisense is not a big deal, you can get the intellisense by casting your ViewBag variable to your model at the top of your view.
But the best approach would be to use the ViewModel,ViewModel is a class just like Model class but it contains some extra properties required only for business process(Specific to that view).
For example following new ViewModel can be created which can contain multiple models and parameters as per the requirement and pass it your view.This way you can access the feature of the model i.e(Validation through DataAnnotation,intellisense etc)

public class MyViewModel
{
public List<ModelOne> ListOfMyModelOne {get;set;}
public ModelTwo MyModelTwo {get;set;}
public string MyModelListTiltle {get;set;}
}

Replied by: | Replied on: Oct 17, 2018



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview