Two actions in the same View

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 there are two Views of different controllers, so when I call 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 that 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 both pages working perfectly (in each separate view), I only need one to appear in a modal, on the same page.

A partial, is a different model, with the functionality of the user save in the bank, some divergence in their information, for the administrator to review and change, if it is correct. The entities User and Divergent have no relationship, I need only show through a Modal, to make the application easier to use by the user.

Just adding, I have both 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 right? Or is there another way to get the same result?

    
asked by anonymous 13.02.2015 / 18:12

2 answers

2

Assuming 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 Modal to receive parameters from action from form . As it is, form can be filled in wrong.

I do not know where the form will go, but I'll assume it is Action Incluir DivergenteController :

@using (Html.BeginForm("Incluir", "Divergente")) { ... }
    
13.02.2015 / 19:11
1

Your View is using Model of Type PortalRH.DomainModel.Entities.Usuario , however its PartialView is expecting Model of Type PortalRH.DomainModel.Entities.Divergente . Since you did not specify the Model to be passed through your View to PartialView , it is passing its Model to be shared with PartialView .

So all you need to do is pass Model from type PortalRH.DomainModel.Entities.Divergente to 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);
    
13.02.2015 / 18:27