Send a modelView as parameter to a modal bootstrap

Asked

Viewed 1,055 times

0

When searching for how to render a partialView in a modal bootstrap, found this link: Using Bootstrap Modal window as Partialview, only I need to make a request POST by clicking the button that opens the modal. That is, I need to send the modelView current of my main view to the partial view.

Could someone tell me how to adapt the "Getgamelisting" example from the link above to achieve my goal?

EDIT 1: Adding Code.

Model:

public class SearchByCooperator
{
    [DisplayName("Nome")]
    public string m_Nome { get; set; }
    [DisplayName("Cota")]
    public string m_Cota { get; set; }
    [DisplayName("Cartão")]
    public string m_Cartao { get; set; }
    [DisplayName("Cidade")]

    public string m_Cidade{ get; set; }
    public string m_LojaAtendimento { get; set; }

    [DisplayName("Acompanhantes")]
    public List<NomeIdCooperado> m_Acompanhantes { get; set; }

    [DisplayName("Data Visita")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime m_DataVisita { get; set; }
}

Controller:

    [HttpPost]
    public ActionResult GerarEtiquetas(SearchByCooperator v_model)
    {

    }

View:

<div class="span">
<button id='gerar'>Gerar</button>
</div>
<!-- Modal -->
<div class="modal fade " id="myModal" tabindex="-1" role="dialog" aria-     labelledby="myModalLabel" aria-hidden="true" style="width: 750px; margin-left: -375px;"     data-url='@Url.Action("GerarEtiquetas")'>
<div class="modal-dialog modal-lg">
<div class="modal-content" id="etiqueta_container">

</div>
</div>
</div>

Script:

  $('#gerar').click(function () {
  var url = $('#myModal').data('url');
  var data = '@COOPA.Core.Common.JsonUtil.ToJson(Model)';

            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(data),
                contentType: 'application/json; charset=utf-8',
                dataType: "json",
                success: function (data) {
                    $('#etiqueta_container').html(data);
                    $('#myModal').modal('show');
                },
                error: function (args) {
                    alert("erro");
            }

            });
        });
  • have tried instead of using the $.get() use the $.post()?

1 answer

1

I don’t know exactly what that line does:

var data = '@COOPA.Core.Common.JsonUtil.ToJson(Model)';

But data need to have the following to work:

{
    'm_Nome': 'Um nome qualquer',
    'm_Cota': 'Cota',
    'm_Cartao': 'Número do cartão',
    'm_Cidade': 'Id ou nome da cidade',
    'm_LojaAtendimento': 'Id ou nome da loja de atendimento'
}

The Controller is okay.

Javascript, therefore, stays like this:

$('#gerar').click(function () {
    var url = $('#myModal').data('url');
    var data = '@COOPA.Core.Common.JsonUtil.ToJson(Model)';

    $.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify(data),
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            success: function (data) {
                $('#etiqueta_container').html(data);
                $('#myModal').modal('show');
            },
            error: function (args) {
                alert("erro");
            }
        });
    });
  • Thanks Gypsy, it worked perfectly. I can receive the event in the controller, I only have one problem yet. My model arrives null in the controller. I searched the internet, and I couldn’t find examples that send the complete OBEJTO, only some hard coded data. This is possible?

  • Serialize your object in hand JSON. Please edit the question and put the statement of your Model so I update the answer to you.

  • I added the code. I think the problem is in my JS, where I try to serialize directly by the "Return new Javascriptserializer() method. Serialize(Model);" my model. From what I understand, it has to be on the right arm?

  • Yes, to be sure binding of the properties of Model.

Browser other questions tagged

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