Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok i've tried it seems everything i possibly can to make this work, i've got another ajax request which works fine. but this one won't. The paramater passed in is always null.

i inspect the ajax request in browser and the params are the ones in the data field of ajax request, but for some reason the params at the controller method are null. But i've done the method the same way as i have in another ajax request and that one works. I've tried setting dataType: json contentType: 'application/json' and that sort of thing with no prevail, only difference is one accepts a model, and the other a viewModel, but this shouldn't make a difference as its the property names it tries to map correct?

Controller method >

C#
public string CreateNote(IT_Note note){
   IT_Note taskNote = new IT_Note();
   taskNote.DateCreated = DateTime.Now;
   taskNote.CreatedByUserID = db.Users.Where(u => u.UserName == User.Identity.Name).Single().ID;
   taskNote.IT_TaskID = note.IT_TaskID;
   taskNote.Note = note.Note;
   db.IT_Notes.Add(taskNote);
   db.SaveChanges();

   TaskViewModel model = new TaskViewModel();
   model.AllUsers = db.Users.ToList();
   model.Task = db.IT_Tasks.FirstOrDefault(t=> t.IT_TaskID == note.IT_TaskID);

   return RenderPartialViewToString("_AllNotes", model);
}


javascriptCode (tried using hardcoded, and the declared variables):

JavaScript
var _submitNote = function () {
   var url = window.location.href;
   var taskID = parseInt(url.match(/\d+\.?\d*/g)[1]);
   var postData = {
      IT_TaskID: 1,
      Note: $('#note').val()
      };
   $.ajax({
      url: "/Tasks/CreateNote",
      type: "GET",
      data: {
         IT_TaskID: 1,
         Note: "hello ball"
      },
      success: function (response) {
         $('#dialog').hide();
         $('#notes').html(response);
      }
   });
};

the IT_Note Model:
C#
public class IT_Note
{
   [Key]
   public int ID { get; set; }
   public string Note { get; set; }
   public int CreatedByUserID { get; set; }
   public int IT_TaskID { get; set; }
   public DateTime DateCreated { get; set; }
}
Posted
Updated 22-Dec-14 6:41am
v3

i changed the paramaters on the action in controller to accept string values rather than a model and works fine now, guess should have done that from the start as passing only two values.
 
Share this answer
 
You need to passthe postdata through the data. Like:-
JavaScript
var _submitNote = function () {
        var url = window.location.href;
        var taskID = parseInt(url.match(/\d+\.?\d*/g)[1]);
        var postData = {
            IT_TaskID: 1,
            Note: $('#note').val()
            
        };
        $.ajax({
            url: "/Tasks/CreateNote",
            type: "GET",
            data: postData,
            success: function (response) {
                $('#dialog').hide();
                $('#notes').html(response);
            }
        });
 
    };

as the parameter expects the model or object of IT_Note.
Please try with this.
Hope this helps.
Post back your queries if any.
Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900