I try to list data provided models data sent from a SearchController. But, I got this error, that I can't find how to fix it.
Only one 'model' statement is allowed in a file.
Here is the code causes the error :
@if (ViewBag.Type == "nomPoste")
{
@model IEnumerable<Monitoring.Models.PosteModel>
if (Model != null)
{
foreach (var item in Model)
{
//...
}
}
if (Model == null)
{
//...
}
}
@if (ViewBag.Type == "nomApplication")
{
@model IEnumerable<Monitoring.Models.AppMetierModel>
if (Model != null)
{
foreach (var item in Model)
{
//...
}
}
if (Model == null)
{
//...
}
}
How do I to fix it, please ?
to achieve this you should try like this
public class MainPageModel
{
public PosteModel Model1{get; set;}
public AppMetierModel Model2{get; set;}
}
Only one 'model' statement is allowed in a file.
You can either
Sample:
if (...)
return View("View1", model1);
else
return View("View2", model2);
@model IEnumerable
) and call sub-view which will use particular type as model:Sample:
@if (ViewBag.Type == "nomApplication"))
{
@Html.Partial("ViewForApplications", Model)
}
else
{
@Html.Partial("ViewForWahtever", Model)
}
And in each partial view you can specify model type:
// ViewForApplications
@model IEnumerable<Monitoring.Models.AppMetierModel>
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With