0
Greetings,
I have a View Index passing values from column "id" to another View Index, as code below:
<a data-bind="attr: {href: url()}" target="_blank" href="@Url.Action("Index","Vw_RegistroVisita", new { id = item.id })">
<i class="fas fa-street-view" title="Visitas"></i>
</a>
The Code below is View 2 that receives the value of the "id" column and does the search:
@using (Html.BeginForm())
<p>
<div class="row">
<div class="col-lg-3">
@Html.TextBox("id", null, new { @class = "form-control", @placeholder = "Digite o nome", style = "width: 260px" })
</div>
<div class="col-lg-1">
<button class="btn btn-outline-primary" type="submit"><i class="fas fa-search"></i></button>
</div>
</div>
</p>
And this is Controller 2 from View that receives the values and does the search:
// GET: Vw_RegistroVisita
public ActionResult Index(Vw_RegistroVisita registroVisita, int id)
{
var estrangeiro = db.Vw_RegistroVisita.Find(id);
ViewBag.id = new SelectList(db.Vw_RegistroVisita, "id", "nome", registroVisita.id);
return View(db.Vw_RegistroVisita.ToList());
}
So far everything is fine. The value I select in View Index #1 appears in Textbox in View Index paragraph 2.
Now my cry for help: how do I use this value, which the Index n°1 passed to Index 2, and do a search and return the data on Index paragraph 2?
your question is confused... if you have the "id" value in controller 2, just search using this "id". This is as far as we can tell, we don’t know where the data comes from, if it’s from a database (or which one), if it uses some class or framework (ADO, Entity Framework, etc.), a service, etc., it is very difficult to say something else
– Ricardo Pontual
Oops! A thousand excuses! The data comes from a Microsoft SQL database and use Entity Framework. My difficulty is that I don’t know how to use "id" to search. I’m new to programming.
– DP WS Receptivo
Only use the method
Find
of Entity Framework. Imagine your entity was "Client", the command would be something likevar cliente = db.Cliente.Find(id)
– Ricardo Pontual
I need to change something in View’s code?
– DP WS Receptivo
I haven’t seen your view, but I don’t think so, you just want to show off, no matter who’s right? just put the code in the controller to retrieve the correct object by id
– Ricardo Pontual
This error appeared: "The model item inserted in the dictionary is type'mt2414_v6.Models.Dataset.Vw_registrovisita', but this dictionary requires an item of type 'System.Collections.Generic.Ienumerable`1[mt2414_v6.Models.Dataset.Vw_registrovisita]'." I edited the post as Controller and View
– DP WS Receptivo
The view is expecting an object and not a list. If you recover an object per ID, you expect only one. To test try changing
db.Vw_RegistroVisita.ToList()
fordb.Vw_RegistroVisita.FirstOrDefault()
– Ricardo Pontual
It didn’t work. It returned the same error. If you can’t "Id" it can also be the "name". I posted an image to help explain better.
– DP WS Receptivo