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

これを使って

$.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 に答える
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 に答える