I have 2 models in MVC , stations & models
public partial class Modelo
{
public Modelo()
{
Scraps = new HashSet<Scrap>();
}
public int Idmodelo { get; set; }
public string? Modelo1 { get; set; }
public virtual ICollection<Scrap> Scraps { get; set; }
}
public partial class Estacion
{
public Estacion()
{
PartEstacions = new HashSet<PartEstacion>();
Scraps = new HashSet<Scrap>();
}
public int Idestacion { get; set; }
public string Estacion1 { get; set; } = null!;
public int Orden { get; set; }
public virtual ICollection<PartEstacion> PartEstacions { get; set; }
public virtual ICollection<Scrap> Scraps { get; set; }
}
a controller
public class ScrapController : Controller
{
private readonly ScrapSystemContext _context;
public ScrapController(ScrapSystemContext context)
{
_context= context;
}
public async Task <IActionResult> Index()
{
return View(await _context.Modelos.ToListAsync()); //Correcto
}
}
And a view, in which I am using Bootstrap, as you can see in the controller I was able to send the values of the model (mvc) Model , but I cannot make the data of the Stations model be sent, I tried to implement the ViewBag property, but in the view I don't know how to import the Stations data without using @model , that if I use 2 in the view, it marks it as wrong, I leave the code of my view and an image of how I have to implement it
View:
@model List<ScrapSystem.Models.Modelo>
<h1 style="color: #ff8000;">Captura</h1>
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="list-group">
<select class="custom-select">
<option selected>Modelo</option>
@foreach(var modelo in Model)
{
<option [email protected](m=>
modelo.Idmodelo)>@Html.DisplayFor(m=> modelo.Modelo1)</option>
}
</select>
</div>
</div>
<div class="col-md-6">
<div class="list-group">
<select class="custom-select">
<option selected>Correciones y anomalias</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
</div>
</div>
<br />
<div class="col-lg-2 col-md-6">
<button type="button" class="btn btn-primary btn-lg" style="background:
#ff8000;">
Enviar
</button>
</div>
<div class="col-lg-2 col-md-6">
</div>
<div class="col-lg-2 col-md-6">
</div>
</div>
</div>
</div>
Come back with the solution to my problem, create a new model, a controller and a new view, called Capture where it contained the attributes that I needed from the Model & Station models plus add those from a third model that I saw that would also be used for the view In the future, I leave the model below:
After I built the controller, use the ViewBag properties, use the _context variable, which is the context of the connection to my DB:
And in the view, create 3 variables in the constructor, create a list object of the models I reference, and then cast the ViewBag like I did in the controller:
To call the list in the form, I did it with the Html.Displayfor statement and the use of a foreach:
I add an illustrative image, in addition to leaving the links to references that I use:
https://www.mejorcodigo.com/p/65952.html
https://stackoverflow.com/questions/19843657/pass-model-to-view-using-viewbag
https://stackoverflow.com/questions/9465376/when-should-i-use-html-displayfor-in-mvc
I think "ViewData" is what you are looking for. In your controller you can do something like the following:
And in your view the following is enough:
Cheers