Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple model in view in ASP.NET MVC?

Problem is: I want to run 3 different actions but instead of that i want to fed all data from single action in a bigger model.

I am using:

public class SearchScrapClass
    {
        public WClass WClass { get; set; }
        public SClass SClass { get; set; }
        public YClass YClass { get; set; }
    }

    public class WClass 
    {
        public string title { get; set; }
        public string link { get; set; }
    }
    public class SClass 
    {
        public string title { get; set; }
        public string link { get; set; }
    }
    public class YClass 
    {
        public string title { get; set; }
        public string link { get; set; }
    }

I am using LINQ to add data in these models.

I am using :

      var wikians = from info in document.DocumentNode.SelectNodes("//div[@id='span']")
        from link in info.SelectNodes("div//a").Where(x => x.Attributes.Contains("href"))
         select new SearchScrapClass //Main Bigger Class
         {
            WClass.link= link.Attributes["href"].Value, //ERROR: How to add to WClass's url ?
            WClass.title= link.InnerText //ERROR: How to add to WClass's url ?
         }


var wikians = from info in document.DocumentNode.SelectNodes("//div[@id='results']")
               from link in info.SelectNodes("p//a").Where(x => x.Attributes.Contains("href"))
               select new SearchScrapClass //Main Bigger Class
                 {
                   YClass.link= link.Attributes["href"].Value, //ERROR: How to add to YClass's url ?
                   YClass.title= link.InnerText //ERROR: How to add to YClass's url ?
                 }

//Also for the 3rd class (model)


    return View(wikians); //and then return bigger class model so that i can access them in view

This is one way i want to add data to link and title of all the classes.

My try is to add data to all 3 classes from different sources and pass the bigger model to view so that i can access all the classes as:

@model SearchScrapClass
@using(Html.BeginForm()) {
    @Html.EditorFor(o => o.WClass.link)
    ...
}

Please suggest a way

Thanks

like image 279
Man8Blue Avatar asked Jun 05 '12 16:06

Man8Blue


People also ask

Can we use 2 models in a view?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.

How do you pass models in view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

Can one view have multiple controllers?

Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. By doing like this, you can automatically make the view available across multiple controllers.


2 Answers

To Expand on my comment, I would suggest creating a ViewModel folder for organization sake. in this add the view model

public class SearchScrapClassViewModel
{
    SearchScrapClass searchScrap;
    WClass wClass;
    SClass sClass;
    YClass yClass;
}

In your controller then you instantiate the new viewmodel

SearchScrapClassViewModel model = new SearchScrapClassViewModel
{
    ....add in your logic to fill your class objects here


}

return view(model);

then in your view add the using for the viewmodel.

@using SearchScrapClassViewModel
like image 60
Brian Avatar answered Sep 22 '22 06:09

Brian


You can pass multiple model by creating a new model class which will contain multiple objects.

public class MultiModel 
    {
     SearchScrapClass searchScrap;
     WClass wClass;
     SClass sClass;
     YClass yClass;
    }
like image 39
Imran Rashid Avatar answered Sep 19 '22 06:09

Imran Rashid