我的参数仍然是Ajax到MVC的null [英] My parameters are still null Ajax to MVC
问题描述
好的,我已经尝试过,似乎我可以做的一切工作,我有另一个ajax请求,工作正常。但这个不会。传入的参数始终为空。
i在浏览器中检查ajax请求,并且params是ajax请求的数据字段中的参数,但由于某种原因,params在控制器方法是null。但是我已经按照我在另一个ajax请求中的方式完成了该方法并且该方法有效。我已经尝试过设置dataType:json contentType:'application / json'和那种没有优势的东西,唯一的区别是一个接受一个模型,另一个是一个viewModel,但这不应该因为它的属性而有所不同它试图映射的名称是否正确?
控制器方法>
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)。单跨度>()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(尝试使用硬编码和声明的变量):
var _submitNote = function (){
var url = window 。 location .href;
var taskID = parseInt (url.match(/\d+ \。 \\ d * / g)[ 1 ]);
var postData = {
IT_TaskID: 1 ,
注意: $(' #note')。val()
};
$ .ajax({
url: / Tasks / CreateNote,
类型: 获取,
数据:{
IT_TaskID : 1 ,
注意: hello ball
},
成功: function (响应){
$(' #dialog')。hide();
$('' #notes')。html(response);
}
});
};
IT_Note型号:
public class IT_Note
{
[Key]
public int ID { get ; set ; }
public string 注意{ get 跨度>; set ; }
public int CreatedByUserID { get 跨度>; set ; }
public int IT_TaskID { get 跨度>; set ; }
public DateTime DateCreated { get ; set ; }
}
(' #note')。val()
};
.ajax({
url: / Tasks / CreateNote,
type: 获取,
数据:{
IT_TaskID: 1 ,
注意: hello ball
},
成功: function (响应){
(' #对话框'跨度>)隐藏();
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 >
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):
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:
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; }
}
('#note').val() };
.ajax({ url: "/Tasks/CreateNote", type: "GET", data: { IT_TaskID: 1, Note: "hello ball" }, success: function (response) {
('#dialog').hide();
这篇关于我的参数仍然是Ajax到MVC的null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!