Skip to content Skip to sidebar Skip to footer

Send Data From Textbox With Ajax To Asp.net Mvc 5 Controller

I need some help. I write little app using ASP.NET MVC5 with JavaScript, jQuery, Ajax... and I can't send data from javascript to MVC Controller and change the model. ViewModel pu

Solution 1:

Javascript Code:

function keystroke() {
var a = 0, b = 0;
    $('#search').keyup(function (event) { a = 1; });

    $('#search').keydown(function (event) { b = 1; });

    $("#search").keypress(function (event) {
        if (e.which === 13) {
            e.preventDefault();
            $('form').click(function () {
                var text = $("#search").val()
                sendForm(a, b, text);
                return false;
            });
        }
    });

};
function sendForm(a, b, text) {
    var data = {FirstInt: a,SecondInt: b,SearchString: text}
    $.ajax({
        url: 'Home/Index',
        type: 'POST',
        contentType: 'application/json',
        data: data,
        success: function () {
            alert('success');
        }
    });
};

Controller Code

[HttpPost]
public async Task<ActionResult> Index(MyViewModel model)
    {
        //...
        if (model.SearchString != null)
        {
            //...
            var a = model.FirstInt;
            var b = model.SecondInt;
        }
        //...
        return View(model);
    }

Post a Comment for "Send Data From Textbox With Ajax To Asp.net Mvc 5 Controller"