From the View how to get Model in Partial View

0

In View main, Index.cshtml , I load a partial view where it has a model . How do I make the Index call this model so I can serialize and send it to an action controller ?

Follow the example below:

View Principal.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 DefinitionArchitecture.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);
        }
...
    
asked by anonymous 05.11.2015 / 12:44

1 answer

1

I still do not understand why, but the correct way to send is by transforming Partial content into form fields. It can 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 since we're dealing with a list of elements, in addition to passing everything to form , you'll need to use NuGet BeginCollectionItem package . It 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 .

    
05.11.2015 / 16:06