Mvc3 How can I read on a razor a list created by joining two tables?

I have this mvc3 project, I am actually studying, and here I am joining two tables from the same database, but I don’t know how I can return it to the view, but in the view I don’t know how to read it.

public ActionResult Index() { ViewBag.Message = "List of Domain Names already crawlered"; domainNamesEntities de = new domainNamesEntities(); var datos = from nombres in de.tableDomNames join titulos in de.tableTitleNames on nombres.domName equals titulos.domName select new { Dominio = nombres.domName, Titulo = titulos.titleN }; return View(datos.ToList()); } 

And here is my problem:

  @model ??????????????? @{ ViewBag.Title = "Index"; } <h2>Index</h2> @foreach (var item in collection???????) { } 

Any ideas?

thanks!

+4
source share
1 answer

The datos variable is an anonymous type. Although you can pass this into a view, you cannot specify a strong type for the model in the view.

If you need a strong type, create a helper object containing your properties, for example.

 public class MyHelper { public string Dominio { get; set; } public string Titulo { get; set; } } 

and use

 select new MyHelper { Dominio = nombres.domName, Titulo = titulos.titleN }; 

In the view you must specify

 @model IEnumerable<MyHelper> @foreach (var item in Model) // or MyHelper item in Model 
+4
source

Source: https://habr.com/ru/post/1445836/


All Articles