0

我有以下 JavaScript 代码:

App.Views.AddPerson = Backbone.View.extend({
        el: '#addPerson',

        events: {
            'submit': 'submit',
        },

        submit: function (e) {
            e.preventDefault();
            var newPersonName = $(e.currentTarget).find('input[type=text]').val();
            var person = new App.Models.Person({ name: newPersonName, age: newAge, occupation: newOccupation });
            this.collection.add(person);
            this.$('#todo-title').val('');

            $.ajax({
              type: 'POST',
              url: '@Url.Action("InsertRecord", "Home")',
              data: newPersonName ,
              success: function(data) {
                alert(data);
              },
              error: function(){
                alert("error");
              }
            });
        }
    });

在控制器中,我有以下方法:

[HttpPost]
    public ActionResult InsertRecord(string str)
    {

        return View();
    }

但它不执行 ajax 调用并引发错误

4

3 回答 3

0
  $.ajax({
            type: 'POST',
            url: window.location + "Home/InsertRecord",
            data: {ID : newPersonName,
                  Name:newAge,
                  DeptID:newOccupation },
            success: function(data) {
            alert(data);
              },
              error: function(){
              alert("error");
              }
           });
于 2013-10-15T08:37:38.923 回答
0

尝试这个:

 submit: function (e) {
        e.preventDefault();
        var newPersonName = $(e.currentTarget).find('input[type=text]').val();
        var person = new App.Models.Person({ name: newPersonName, age: newAge, occupation: newOccupation });
        this.collection.add(person);
        this.$('#todo-title').val('');

       $.ajax({
        type: 'POST',
        url: window.location + "Home/InsertRecord",
        data: {ID : newPersonName},
        success: function(data) {
        // Code after success },
          error: function(){
          alert("error");
          }
       });
于 2013-10-17T03:47:21.533 回答
0

用这个

$.ajax({
            type: 'POST',
            url: '@Url.Action("InsertRecord", "Home")',
            data: {str:newPersonName} ,  // use the same paramtre name as in Controller
            success: function(data) {
            alert(data);
              },
              error: function(){
              alert("error");
              }
           });

希望能帮助到你

于 2013-10-15T08:17:08.873 回答