Two actions in the same View

Asked

Viewed 1,155 times

0

I have a View where I return the data of a user, and I need to call another view, to save data, through a modal. The problem, is that they are two views of different controllers, so when calling, by means of a Partialview i get a declaration error.

The model item passed into the dictionary is of type 'PortalRH.DomainModel.Entities.Usuario', but this dictionary requires a model item of type 'PortalRH.DomainModel.Entities.Divergente'.

My index:

@model PortalRH.DomainModel.Entities.Usuario
<!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        Relatar Divergência
    </button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">
                    @Html.Partial("~/Views/Divergente/Relatar.cshtml")
                </div>
            </div>
        </div>
    </div>

    <!--ABAS-->
    <div class="container">
        <div class="col-lg-12">
            <ul class="nav nav-pills faq-cat-tabs">
                <li class="active"><a data-toggle="tab" href="#sectionA">Pessoal</a></li>
                <li><a data-toggle="tab" href="#sectionD">Documentos</a></li>
                <li><a data-toggle="tab" href="#sectionB">Endereço</a></li>
                <li><a data-toggle="tab" href="#sectionC">Dados Profissionais</a></li>
            </ul>
        </div>
        <div class="col-lg-12">
            <div class="tab-content">
                <div id="sectionA" class="tab-pane fade in active">
                    <br /><br />
                    @Html.Partial("_DadosPessoais")
                </div>
                <div id="sectionB" class="tab-pane fade">
                    <br /><br />
                    @Html.Partial("_Endereco")
                </div>
                <div id="sectionC" class="tab-pane fade">
                    <br /><br />
                    @Html.Partial("_DadosProfissionais")
                </div>
                <div id="sectionD" class="tab-pane fade">
                    <br /><br />
                    @Html.Partial("_Documentos")
                </div>
            </div>
        </div>
    </div>




    <div class="container body-content">

        <hr />
        <div align="center">
            <footer>
                <p>&copy; @DateTime.Now.Year - Portal RH - <a href="http://www.vilavelha.es.gov.br" target="_blank">Prefeitura Municipal de Vila Velha</a></p>
            </footer>
        </div>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")

The View I need to call through the modal:

@model PortalRH.DomainModel.Entities.Divergente

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="row">
        <div class="col-md-2">
            @TempData["Mensagem"]
        </div>
        <div class="col-md-8">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h5><strong>Dependente(s)</strong></h5>
                </div>

                <table class="table">
                    <thead>
                        <tr>
                            <td></td>
                            <th>CAMPO</th>
                            <th>INFORMAÇÃO CORRETA</th>
                        </tr>
                    </thead>
                    <tbody>

                        <tr>
                            <td colspan="3" align="center">DADOS PESSOAIS</td>
                        </tr>
                        <tr>
                            <td><input type="checkbox" class="cb" data-id="nmMae"></td>
                            <td> Data de Nascimento</td>
                            <td><input type="text" data-id="nmMae" disabled class="form-control" name="nmMae"></td>
                        </tr>

                    </tbody>

                </table>
            </div>
        </div>
    </div>



    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
}

Just adding, I have the two pages working perfectly( in each separate view), I only need one to appear in a modal, on the same page.

partial is a different model, with user functionality saved in the database, some divergence in its information, for the administrator to review and change, if correct. The User and Divergent entities have no relationship, I just need to show through a Modal, to make the application easier to be used by the user.

Just adding, I have the two pages working perfectly( in each separate view), I only need one to appear in a modal, on the same page.

Am I trying to do it the right way? Or is there another way to get the same result?

  • The information that will be displayed on PartialView are in which Model?

  • @Ciganomorrisonmendez I edited the question, with the models in their certain Views

  • I still don’t understand. Index has Divergente as Model. To Partial also?

  • @Ciganomorrisonmendez No. Index has User as Model, and partial has Divergent. I need to show both in the same view, just to make it easier for the user to use the application.

  • And what is the relationship of Usuario with Divergente, if there is any?

  • @None. What I’m wanting to do is when the person clicks the button REPORT DIVERGENCE open a modal with the "Divergent" view, so the user can edit it. Because the way it is, it is redirected to another page.

Show 1 more comment

2 answers

2


Assuming that Modal is already properly hidden, the following statement should resolve:

<div class="modal-body">
    @Html.Partial("~/Views/Divergente/Relatar.cshtml", new Divergente())
</div>

Update your @Html.BeginForm() from within the Modal to receive parameters from the action of form. The way it is, the form can be filled wrong.

I don’t know where the form will go, but I’m gonna assume that’s the Action Incluir of DivergenteController:

@using (Html.BeginForm("Incluir", "Divergente")) { ... }
  • Now that I’ve noticed something curious, his Partialview doesn’t use @model, will understand.

  • That’s just what I needed. @Html.Beginform.()? I don’t understand where I’m supposed to change.

  • The @Html.BeginForm() has some overwritten, one of them allows you to inform the actionName and the controllerName, in your case it is interesting to inform these values so that the form is received by the right Action.

  • @Renilsonandrade I updated the answer.

  • Thank you. That’s exactly what I needed

1

Your View is using a Model kind PortalRH.DomainModel.Entities.Usuario, however its PartialView is expecting a Model kind PortalRH.DomainModel.Entities.Divergente. As you did not specify the Model to be passed down your View to the PartialView, he’s passing his own Model to be reconciled with the PartialView.

So all you need to do is pass one Model of the kind PortalRH.DomainModel.Entities.Divergente for your PartialView.

Controller

public ActionResult Index(...) 
{
    ...
    var divergente = default(Divergente);
    divergente = this.GetDivergente();
    ViewBag.Divergente = divergente;
    ...
}

public Divergente GetDivergente()
{
    ...
}

View

@Html.Partial("~/Views/Divergente/Relatar.cshtml", ViewBag.Divergente as PortalRH.DomainModel.Entities.Divergente);
  • I forgot to mention that you have a @model Portalrh.DomainModel.Entities.Usuario in my Index view. it accepts two @models?

  • and the @model Portalrh.DomainModel.Entities.Divergent I already own in my partial

  • @Renaldo, @model is a model statement that the View/PartialView expects to receive. As in PartialView you stated that you would use a model of the type Divergente, then its View need to pass an object of the type Divergente as Model for your PartialView

  • If your object Usuario owned as type property Divergente, you can call your View as follows: @Html.Partial("~/Views/Divergente/Relatar.cshtml", Model.Divergente);, otherwise, still in your control you will need to save an object Divergente in his ViewBag, and use it to call PartialView

  • Divergent is another class, where it has no relationship with the User. I just need to show it in a modal, to make it easier for the user to use this functionality

  • updated the answer to use a Viewbag.

  • With this code, I’m getting an error in this Getdivergente, as if it didn’t exist.

  • you need to implement this method.

Show 3 more comments

Browser other questions tagged

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