SNIPER512
SNIPER512
CC#
Created by SNIPER512 on 6/6/2024 in #help
Getting checked for username instead of email attribute in Identity framework in ASP.NET Core MVC
I wanted to ask the question here but the character limit won't let me. I have posted it on Stack Overflow. Please answer this. https://stackoverflow.com/questions/78583462/getting-checked-for-username-instead-of-email-attribute-in-identity-framework-in
3 replies
CC#
Created by SNIPER512 on 6/1/2024 in #help
Can't get Data in Controller by Ajax from View in dotnet MVC Core
I am trying to pass data from a view to my controller. I am getting the values in Ajax correctly, but I am unable to pass them to the controller. I get the comment in controller as null always. Ajax code from view:
function handleCommentSubmitClick(PostID) {
console.log("PostID: ", PostID);
const comment = $("textarea[name='post-comment']").val();
$.ajax({
type: "POST",
url: "/PostCommentInput",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
comment: comment
},
success: function (data, status) {
console.log(data);
},
error: function (xhr, status, error) {
console.error("AJAX error: " + status + ' ' + error);
}
});
}
function handleCommentSubmitClick(PostID) {
console.log("PostID: ", PostID);
const comment = $("textarea[name='post-comment']").val();
$.ajax({
type: "POST",
url: "/PostCommentInput",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
comment: comment
},
success: function (data, status) {
console.log(data);
},
error: function (xhr, status, error) {
console.error("AJAX error: " + status + ' ' + error);
}
});
}
Controller code:
[HttpPost("/PostCommentInput")]
public IActionResult PostCommentInputHandler(string comment)
{
Console.WriteLine("Comment: " + comment);
return Json(new { status = comment });
}
[HttpPost("/PostCommentInput")]
public IActionResult PostCommentInputHandler(string comment)
{
Console.WriteLine("Comment: " + comment);
return Json(new { status = comment });
}
I have seen few solutions here in stack overflow but nothing is working for me. In my project, I am doing this at some point and its working there:
const likeClickHandler = (PostID) => {
$.ajax({
type: "POST",
url: "/Like/",
ontentType: "application/json; charset=utf-8",
dataType: "json",
data: { PostID: PostID },
success: function (data, status) {
successFunc(data, status);
},
error: errorFunc
});
const likeClickHandler = (PostID) => {
$.ajax({
type: "POST",
url: "/Like/",
ontentType: "application/json; charset=utf-8",
dataType: "json",
data: { PostID: PostID },
success: function (data, status) {
successFunc(data, status);
},
error: errorFunc
});
Controller:
[HttpPost("/Like")]
public IActionResult LikeHandler(long PostID)
{
string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
UserPostRepository repo = new UserPostRepository();
List<int> Counts = repo.PostLike(PostID, userId);
return Json(new { postIDController = PostID, Counts = Counts });
}
[HttpPost("/Like")]
public IActionResult LikeHandler(long PostID)
{
string userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
UserPostRepository repo = new UserPostRepository();
List<int> Counts = repo.PostLike(PostID, userId);
return Json(new { postIDController = PostID, Counts = Counts });
}
4 replies