From the View how to get the Model in Partial View

Asked

Viewed 1,369 times

0

In the View leading, Index.cshtml, carry a partial view where this has a model. How to make Index call this one model so that I can serialize and send to a action in the controller?

Below is an example:

View Main.cshtml

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>Glossário de Termos de Arquitetura
                    de Software</h1>
            </hgroup>
        </div>
    </section>
}

<script type="text/javascript"
    src="~/Scripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript">

    function atualizarDefinicao() {
        var model = @Html.Raw(Json.Encode(Model));  //<--- aqui: como pegar Model que está na parcialView? ou seja preciso pegar os campos que estão na partial view
        $.ajax(
        {
            type: 'POST',
            url: '/Index/DefinicaoArquitetura',
            data: JSON.stringify(model), //aqui envio para a action do controller
            contentType: "application/json",
            dataType: 'html',
            cache: false,
            async: true,
            success: function (data) {
                $('#definicaoArquitetura').html(data);
            }
        });
    }

    $(document).ready(function () {
        setInterval(atualizarDefinicao, 30000);
    });

</script>

<p>
    Última atualização completa desta página:
        @DateTime.Now.ToString("HH:mm:ss")
</p>

<h3>Definição:</h3>
<form id="myform">
<div id="definicaoArquitetura">

</div>
</fom>

<input id="btnProximaDefinicao"
    type="button"
    value="Próxima Definição"
    onclick="atualizarDefinicao();" />

Partial View Definicaoarquitetura.cshtml

@model IEnumerable<Sorteio.Models.MatrizSorteioModel>


<table width="100%" class="table" border="1" cellpadding="0">
    <tr>
        <th class="fonte">
            &nbsp;
        </th>
        <th >
            @Html.DisplayNameFor(model => model.coluna_1)
        </th>
        <th >
            @Html.DisplayNameFor(model => model.coluna_2)
        </th>
        <th >
            @Html.DisplayNameFor(model => model.coluna_3)
        </th>
        <th >
            @Html.DisplayNameFor(model => model.coluna_4)
        </th>        
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td >
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_3)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_4)
            </td>                
        </tr>
    }
</table>

Homecontroller class

...
public ActionResult DefinicaoArquitetura(List<MatrizSorteioModel> listaMatriz)
        {
            ...

            return PartialView("Tabela", listaMatriz);
        }
...
  • what this method set(...) are you doing? just try to do var dados = @Html.Raw(Json.Encode(Model)); and preferably do it out of method atualizarDefinicao, so that the object does not need to be re-instantiated to each interaction of the method. And in its HomeController, you have a method public PartialResult DefinicaoArquitetura(MyModel model)?

  • Toby. It was a mistake. I gave a better update for your better understanding. What I really need is a way to take the partialView fields to serialize.

  • I don’t understand anything. What is your purpose in this?

  • Gypsy, my Basicamante debt is like taking the Model object that is in Partial View. If this object is in the current page I can pick it up. See the comment in the code.

  • @Leandro, take a look at this example

1 answer

1

I still don’t understand why, but the correct way to send is to transform the content of Partial in fields of form. It may be by <input type="hidden">:

@foreach (var item in Model)
{        
    <tr>       
        <td>
            @Html.DisplayFor(modelItem => item.Id)
            @Html.HiddenFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.coluna_1)
            @Html.HiddenFor(modelItem => item.coluna_1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.coluna_2)
            @Html.HiddenFor(modelItem => item.coluna_2)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.coluna_3)
            @Html.HiddenFor(modelItem => item.coluna_3)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.coluna_4)
            @Html.HiddenFor(modelItem => item.coluna_4)
        </td>
    </tr>
}

But how are we dealing with a list of elements, in addition to passing everything to form, you will need to use the package Nuget Begincollectionitem. Would look like this:

@foreach (var item in Model)
{
    @using (Html.BeginCollectionItem("listaMatriz"))
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
                @Html.HiddenFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_1)
                @Html.HiddenFor(modelItem => item.coluna_1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_2)
                @Html.HiddenFor(modelItem => item.coluna_2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_3)
                @Html.HiddenFor(modelItem => item.coluna_3)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.coluna_4)
                @Html.HiddenFor(modelItem => item.coluna_4)
            </td>                
        </tr>
    }
}

There are several examples of how to use Begincollectionitem here.

  • now I understood why you were not understanding. There was an error in my code. In the code snippet was ... JSON.stringify(data) ... and corrected to ... JSON.stringify(model) ...

  • @Leandro, what he can’t understand is his need to send the model through an AJAX request to fill out a Partialview. I also don’t understand why this.

  • @Leandro Does the answer meet what you need? Should I add something?

  • @I can’t see that now. What I want is: capture the elements of the partial view form, serialize and move to action via ajax. When I get home I will take the test. I’ll let you know. Thank you very much.

Browser other questions tagged

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