How to pass multiple models into ASP.NET MVC?

The problem is this: I want to run 3 different actions, but instead I want to transfer all the data from one action to a larger model.

I use:

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 use LINQ to add data to these models.

I use:

  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 url ? WClass.title= link.InnerText //ERROR: How to add to WClass 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 url ? YClass.title= link.InnerText //ERROR: How to add to YClass 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 to add data to the link and title of all classes.

My attempt is to add data to all 3 classes from different sources and pass a large model for viewing so that I can access all classes as follows:

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

Please suggest a way

thanks

+7
source share
4 answers

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

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

In your controller, you create a new view mode

 SearchScrapClassViewModel model = new SearchScrapClassViewModel { ....add in your logic to fill your class objects here } return view(model); 

then in your view add usage for viewmodel.

 @using SearchScrapClassViewModel 
+8
source

You can transfer several models by creating a new model class that will contain several objects.

 public class MultiModel { SearchScrapClass searchScrap; WClass wClass; SClass sClass; YClass yClass; } 
+1
source

See the tuple tutorial http://www.dotnetperls.com/tuple or this http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx

Like the controller:

 public class HomeController : Controller { public ActionResult Index() { var first = new FirstModel(); var second = new SecondModel(); return View(Tuple.Create(first,second)); } } 

And the view:

@model Tuple

 <div> @Model.Item1.FirstModelProp @Model.Item2.SecondModelProp </div> 
+1
source
 SearchScrapClassViewModel model = new SearchScrapClassViewModel { ....add in your logic to fill your class objects here } 

what logic we apply here: ".... add to your logic to populate your class objects here"

0
source

All Articles