0
I have the following excerpt
<div class="form-group">
<div class="col-lg-6" style="margin-left:15px">
<div class="row">
<div id="Empreendimento" class="form-control">
<label>EmpreendimentoId</label>
@*@ViewBag.Empreendiment=EmpreendimentoId*@
<input type="text" id="Empreendimentoid">
js
/*@ INICIALIZA SELECT @*/
$('document').ready(function () {
$("#Empreendimento").select2({
language: "pt-BR",
containerCssClass: ':all:',
theme: "bootstrap",
ajax: {
url: "/Empreendimentos/GetEmpreendimentos",
dataType: 'json',
delay: 250,
data: function (params) {
return {
search: params.term,
page: params.page || 1,
limit: 12,
sortBy: "text",
direction: "asc"
};
},
cache: true
},
placeholder: 'Buscar Empreendimento...'
});
});
$('#Empreendimento').on('', function (e) {
});
/*@ ADICIONA EVENTO PARA PREENCHER CAMPOS AO SELECIONAR UM EMPREENDIMENTO @*/
$("#Empreendimento").on('select2:select', function (e) {
$("#nome").val(e.params.data.text);
$("#contrato").val(e.params.data.Contrato_Empreendimento);
$("#ordem").val(e.params.data.OrdemDeServico);
var x = $("#Empreendimento").val();
function qualquer() {
return x;
}
document.getElementById("Empreendimentoid").value = x;
and I want to pass this value to the controller via a viewbag or similar.... how to do?
Obs: I want to pass the value inside the pro controller
Editing:
I know that everything is done by http, including because of the Binder model, this should go to the post, with the value of the ID, but it always comes empty.
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Laudo laudo)
{
ViewBag.LaudoId = new SelectList(db.CaracterizacaoRegiaoResidencias, "LaudoId", "LaudoId", laudo.LaudoId);
ViewBag.LaudoId = new SelectList(db.CaracterizacaoResidencias, "LaudoId", "LaudoId", laudo.LaudoId);
ViewBag.LaudoId = new SelectList(db.ComposicaoUnidadeResidencias, "LaudoId", "LaudoId", laudo.LaudoId);
ViewBag.LaudoId = new SelectList(db.Finalizacaos, "LaudoId", "LaudoId", laudo.LaudoId);
ViewBag.EmpreendimentoId = new SelectList(db.Empreendimentoes, "EmpreendimentoId", "Contrato_Empreendimento", laudo.EmpreendimentoId);
if (ModelState.IsValid)
{
db.Laudoes.Add(laudo);
db.SaveChanges();
}
return View(laudo);
}
In order for you to pass any View value to the Controller you will need to make a request to the server, be it GET, POST, PUT or DELETE... I also could not understand what you are trying to represent with the JS excerpt, besides not making sense, it has no relation to the question.
– Leandro Angelo
@Leandroangelo, this is the excerpt, the significant part of the example I did not post the full code, thanks for the information, I will edit the question with my problem right, because I already tried to use the modelbinder to send in the form and could not, so I asked this question.
– Raph Apresentacao
@Raphpresentation when I started I used this article on the Macoratti website: http://www.macoratti.net/15/05/mvc_ajax.htm it is very worth you to read and try to reproduce to understand how it works ;)
– Leonardo Bonetti
Your controller expects a
Laudo
, your view shows only input<input type="text" id="Empreendimentoid">
... I don’t know if in your case it already contemplates the input parameters... Then, so that the bindEmpreendimentoid
, your input needs to display the attributename="Empreendimentoid"
– Leandro Angelo
That’s what I noticed later, that bind acts by name and not id
– Raph Apresentacao
Resolved then?
– Leandro Angelo