Posting form without @HTML.Beginform and using Jquery(ajax) in asp.net MVC

后端 未结 3 560
梦如初夏
梦如初夏 2020-12-30 09:23

How can I fill out a form without using @HTML.Beginform and by using JQuery Ajax instead? Right now I tried:

    var postData = { form1: username, form2: pas         


        
3条回答
  •  梦谈多话
    2020-12-30 09:38

    This will do a xhr post to the server and return the view as data (response) It will not navigate, if it returns html, you need to set a proper datatype in your request to tell that you expect html back from the server:

    Given your action returns html, you can put the returned html on your page in your success function.

    postData = "{'ID':'test'}";
    $.ajax({
        type: "POST",
        url: '/Home/Test',
        data: postData,
        dataType: 'html',
        contentType: 'application/json',
        traditional: true,
        success: function (data) {
            $("#yourdomelement").html(data);
        }
    });
    

    In your action;

    public ActionResult Test([FromBody]PostData id)
    {
        return Content("

    hello

    "); }

提交回复
热议问题