Passing parameters between Views and Listing values

Asked

Viewed 236 times

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?

Images to help inserir a descrição da imagem aqui

  • 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

  • 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.

  • Only use the method Find of Entity Framework. Imagine your entity was "Client", the command would be something like var cliente = db.Cliente.Find(id)

  • I need to change something in View’s code?

  • 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

  • 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

  • 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() for db.Vw_RegistroVisita.FirstOrDefault()

  • 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.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.