I cannot pass view parameters to controller in MVC using URL

Asked

Viewed 82 times

0

I’m doing an MVC project and I’m not getting through to Controller the parameter window that is coming up to View. I’ll pass the two classes, if you need more information see edit here:

List enclosure index.cshtml

@model Affonso_DW.Models.ListaGabineteModels

@{
    Layout = null;
    int cont = 0;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    <title></title>
</head>
<body>
    <div>
        <label for="Gabinete">
            <b>Gabinetes</b><br />
        </label>

        <div id="accordion">
            <div class="card">

                @foreach (var gabinete in Model.ListaGabinete)
                {
                    <div class="card-header">
                        <!-- comandos data-toggle controlam o layout do menu a ser usado-->
                        <a class="card-link" data-toggle="collapse" href="#collapse-@cont">
                            @gabinete.GabineteDW.Name
                        </a>
                    </div>

                    <div id="collapse-@cont" class="collapse" data-parent="#accordion">
                        <b>Janelas de Pesquisa</b><br />
                        @foreach (var janelaPesquisa in gabinete.ListaJanelaPesquisa)
                        {
                            <div class="card-body">
                                <a href="@Url.Action("JanelaPesquisaIndex", "JanelaPesquisa", new { janelaPesquisa = janelaPesquisa })">
                                    @janelaPesquisa.JanelaPesquisaDW.DisplayName
                                </a>
                            </div>
                        }
                    </div>
                    cont++;
                }
            </div>
        </div>
    </div>
</body>
</html>

Janelapesquisacontroller.Cs

public class JanelaPesquisaController : Controller
    {
        JanelaPesquisa janelaPesquisa = new JanelaPesquisa();

        // GET: JanelaPesquisa
        [HttpGet]
        public ActionResult JanelaPesquisaIndex(JanelaPesquisa janelaPesquisa)
        {
            JanelaPesquisaModels janelaPesquisaModels = new JanelaPesquisaModels(janelaPesquisa);

            return View(janelaPesquisaModels);
        }
    }
  • You cannot pass values from "View to Controller" as the question title, or from "Controller to View". Your model in view to be an array (Affonso_DW.Models.ListaGabineteModels) different from the object returned by the controller (JanelaPesquisaModels).

  • I’m trying to do the URL that’s on Listagabineteindex pass as parameter the object to be used in controller Janelapesquisacontroller so that I may thus open the new view with the information of this parameter passed. I was trying to pass a direct object to the controller and inside I created the model which you would be needing. But the correct thing is to already pass the correct model of view to the controller, that would be it?

  • You see, when accessing a URL of your site, the HTTP request goes to Action corresponding to the Controller, then you fill in and return the object of your View. To pass object when accessing a URL, declare the parameter in Action, and on the page you call, create a form containing inputs with the same object properties names. By submitting the form to the action, the properties of the parameter object in the Action will be completed.

No answers

Browser other questions tagged

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