Recently,When I migrate one of my .NET Framework
Application to .NET Core 1.1.Howerver,I meet a question, When I use Ajax in Views to invoke Controller method, but i found Controller can't receive the params. I puzzled. Anyone can help me how to figure out that. Detail below:
Here is code in .NET Core:
Views (html):
<input id="btnSubmit" type="button" style="font-size: 14px; padding: 2px 4px;"
value="OK,Submit!" />
Controller (C#):
[NeedLogin]
public async Task<string> AddQuestion(QuestionItem qes)
{
//...
}
QuestionItem is Entity object.
$.ajax({
type: 'post',
data: '{"qes":' + JSON.stringify(question) + '}',
url: '/q/AddQuestion',
dataType: 'Text',
success: function (data) {
if (data.indexOf("Add Failed") !== -1) {
showDialogCustom("Tips", data);
$("#btnSubmit").prop("disabled", false).val("Ok,Commit!");
} else {
if (!$("#txtQuestionMarkdown").length) {
tinyMCE.get('txtContent').setContent("");
}
$("#txtQuestion,#mySingleField").val("");
jutil.storage.deleteStorage("q_t");
jutil.storage.deleteStorage("q_c");
if (question.SaveOption) {
window.top.location.href = "/q/SaveDraftSuccess?qid=" + data;
} else {
window.top.location.href = "/q/success?qid=" + data;
}
}
},
error: function (msg) {
showDialogCustom("Wrong!", msg.responseText);
$("#btnSubmit").prop("disabled", false).val("Ok,Commit!");
}
});
And I want to know Why my Controller can't receive the qes
params in .NET Core Application?
And I run it successfully in .NET Framework Application.
What the data or dataType should be ?
NeedLogin
pass? Also, you should change dataType toapplication/JSON
rather than text (since it is JSON). You also may have to specify[HttpPost]
on the method and[FromBody]
on the argument ([FromBody] QuestionItem qes
)Chrome Developer Tools
find the qes is not null. So I guess it could be the dataType and ContentType. And last night ,I change datatype toapplication/json
, add[HttpPost]
and[FromBody]
. It doesn't work.question
object?$.ajax
code, some properties inquestion
object is get from the user input.