-1

The idea is simple.

I want to wrap a Json data and using jQuery Ajax to pass the data to controller.

Here is the model definition.

public class SearchModel
{
        public int answerValue;
        public int year;
}

jQuery code:

 $(function() {
            var answerValue = $('#answerValue').val();
            var year = $('#year').val();
            var data = { "answerValue": answerValue, "year": year };

            //data = answerValue;
            $('#btn1').click(function () {

                $.post("home/AnswersSearch", data, function (result) {
                    alert(result);
                });
            });

        });

Code on the controller side:

[HttpPost]
public ActionResult AnswersSearch(SearchModel model)
{
    //do something
    return Content("1");
}
7
  • The ajax method is call a method named AnswersSearch() but all you have shown a method named RespondentAnswersSearch() Commented Jan 16, 2017 at 21:37
  • @Stephen Muecke THanks for the reply! I have changed that, still it doesn't work. Commented Jan 16, 2017 at 21:38
  • So what is the problem? What errors do you get? (and always use '@Url.Action("AnswersSearch", "Home")' to generate the correct url) Commented Jan 16, 2017 at 21:39
  • And I assume SearchModelmodel model is also a typo? Commented Jan 16, 2017 at 21:40
  • 1
    Because they are fields, not properties - add getters and setters - public int answerValue { get; set; } Commented Jan 16, 2017 at 21:41

1 Answer 1

0

try sending back a SearchModel object like so:

var data = { "SearchModel": { "answerValue": answerValue, "year": year } };
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.