Post with no fronend

Asked

Viewed 73 times

0

I’m trying to make a Post of a form info but I don’t quite know how to do.

This is my C form# :

   @using (Html.BeginForm("Enviar", "Mensagem", FormMethod.Post, new { enctype = "multipart/form-data", @class = "", @id = "formNovaMsg" }))
                {
                    <div class="">

                        <div style="display: none;">
                            <input id="LojaId" name="LojaId" value="@Model.LojaId" />
                        </div>
                        <div style="display: none;">
                            <input id="ClienteId" name="ClienteId" value="@Model.Mensagens[0].ClienteId" />
                        </div>


                        <!-- /.box-body -->
                        <div class="box-footer">

                            <div class="input-group">
                                <input type="text" id="Conteudo" name="Conteudo" placeholder="Escreva sua mensagem ..." class="form-control">
                                <span class="input-group-btn">
                                    <button type="submit" id="btnSubmit" class="btn btn-warning btn-flat">Enviar</button>
                                </span>
                            </div>
                        </div>


                        <!-- /.box-footer-->
                    </div>
                }

To be able to give the post I followed an example that resulted in the following function:

    $("#formNovaMsg").ajaxForm({
    type: "post",
    beforeSubmit: function (arr, $form, options) {
        loading();
    },
    success: function (data) {
        if(!data.Erro){
            $("#conversa").append(data.Html);
            $("#formNovaMsg")[0].reset();
            $(".msgsis").fadeOut(300);
            var elem = document.getElementById('conversa');
            elem.scrollTop = elem.scrollHeight;
        }
    }
});

This view is a kind of chat the idea and it send the message and already give the post direct.

When I’m trying this way I’m trying it wrong Uncaught TypeError: $(...).ajax is not a function i checked the Jquery and are not conflicting ( I have only 1 , 1.9.1).

1 answer

1


I suggest you do not give a Ubmit, if it is a "live chat", do it via javascript. For example: change to not give Submit to your html

<button type="button" id="btnSubmit" class="btn btn-warning btn-flat">Enviar</button>

and in your javascript

 $('#btnConcluir').click(function () {
    Teste();
 }

function Teste(){
    var postForm = {
        'exemplo': $('#Conteudo').val() //pega o valor no campo digitado pelo usuário
    };
   // lembrando que 'exemplo' será o nome da variavel recebida como parametro, no C#

    $.ajax({
        url: "/WebService/Exemplo/Chat", //altere para o caminho que você vai querer mandar o post
        type: "post", //tipo post
        data: postForm, //os dados
        success: function (response) {
              alert(response["Mensagem"])
              //se deu certo
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //se deu erro
            console.log(textStatus, errorThrown);
        }
    });
}

Code C#

[HttpPost]
public void Chat(String exemplo)
{
    var resultado = new
    {
         Erro = false,
         Mensagem = exemplo;
    };

    return Json(resultado, JsonRequestBehavior.DenyGet);
}
  • ah intendi , but in the case then I take that Begin Form and leave only one html form even ?

  • Yeah. Try it on and let me know if it works ^^

  • I’m gonna test it now, but still vlw ai cara.

  • I think it worked , but with your example I managed to intender a lot, I’m following an example that uses Viewenginer , but ta returning null, but this problem worked , anything I return with another question, thanks.

Browser other questions tagged

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