Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

public class MyViewModel
{
//...
    public string SearchString { get; set; }
    public int? FirstInt { get; set; }
    public int? SecondInt { get; set; }
}

Javascript

    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 () {
                    sendForm(a, b);
                });
            }
        });
    };
    function sendForm(a, b) {
        $.ajax({
            url: @Url.Action("Index", "Home"),
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                FirstInt: a,
                SecondInt: b
            }),
            success: function () {
                alert('success');
            }
        });
    };

View

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-inline", role = "form" }))
            {
                <div class="form-group has-feedback">
                    @Html.TextBox("SearchString", ViewBag.SearchFilter as string, new
            {
                @class = "form-control",
                onclick="keystroke()",
                id = "search"
            })
                </div>
            }

Controller

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

Help me, please, to send all the values to controller. Those that changed in JavaScript and what I enter in the textbox. Thanks.

share|improve this question
    
url: @Url.Action("Index", "Home") ?? it is not an url – gypsyCoder May 26 '15 at 13:00
    
I try write url: "Home/Index" but it does not work too – Mr. X May 26 '15 at 13:03
    
Is it hit brake point when you send request in debug mode? – genichm May 26 '15 at 13:19
    
Html.BeginForm("Index", "Home", FormMethod.Post... this post works without HttpPost attribute, but I do not get the value from javascript, that's the problem. When I use brakepoint after the var b = model.SecondInt;, a and b are null – Mr. X May 26 '15 at 13:25
    
HttpPost is restriction to "listen" to post requests only, without that attribute the method "listening" for both GET and POST. Do you have "name" attribute in your HTML controls? – genichm May 26 '15 at 13:29
up vote 1 down vote accepted

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);
    }
share|improve this answer
    
When I write some text in textbox and press enter key, SearchString get the value, but a and b are null – Mr. X May 26 '15 at 13:35
    
Plz, show me How do I change the view to only send ajax post? – Mr. X May 26 '15 at 13:39
    
have you tried the given solution? @Mr. X – gypsyCoder May 26 '15 at 13:40
    
does $("#search").keypress execute? – gypsyCoder May 26 '15 at 13:45
    
yes I tried, but most likely you're right and ajax post and FormMethod.Post in conflict – Mr. X May 26 '15 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.