Passing view data to controller with Viewbag/ how to pass jquery script value to viewbag

Asked

Viewed 439 times

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);
    }
  • 2

    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.

  • @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.

  • 1

    @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 ;)

  • 1

    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 bind Empreendimentoid, your input needs to display the attribute name="Empreendimentoid"

  • That’s what I noticed later, that bind acts by name and not id

  • 1

    Resolved then?

Show 1 more comment

1 answer

1


To receive the values of your View in Controller, you need to declare the attribute name. The attribute id serves for javascript and css, if the input does not have a name defined it will not be included in Formdata, it will not be posted to the server and therefore will not be received in the controller.

<input type="text" id="Empreendimentoid" name="Empreendimentoid" />

Browser other questions tagged

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