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">
</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 dovar dados = @Html.Raw(Json.Encode(Model));
and preferably do it out of methodatualizarDefinicao
, so that the object does not need to be re-instantiated to each interaction of the method. And in itsHomeController
, you have a methodpublic PartialResult DefinicaoArquitetura(MyModel model)
?– Tobias Mesquita
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.
– Leandro
I don’t understand anything. What is your purpose in this?
– Leonel Sanches da Silva
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
@Leandro, take a look at this example
– Tobias Mesquita