I am developing an admin page that able to edit registered users info. When I select a user from a dropdownlist, the users current info is filling to edit fields. I made it with Javascript and Json.
This is my JS code so far:
$(document).ready(function () {
$("#UserId").change(function () {
if ($(this).val() != "" && $(this).val() != undefined && $(this).val() != null) {
$.ajax({
type: "POST",
url: "/AdminController/GetUserData",
cache: false,
data: { userid: $(this).val() },
success: function (result) {
$("#EMailAdress").val(result.EMailAdress)
$("#UserName").val(result.UserName)
}
});
}
else {
$("#UserName").val("");
$("#EMailAdress").val("");
}
});
});
As you see I am trigering "/AdminController/GetUserData" and I have this code in there:
[HttpPost]
public JsonResult GetUserData()
{
int userid = Convert.ToInt32(Request.Form["userid"]);
MyContext _db = new MyContext();
var userObj = _db.Users.SingleOrDefault(p => p.UserId == userid);
return Json(userObj);
}
Now I have to display the selected user's current role in same page. I can reach that role data in my controller. But I can't find a way to add it to my JsonResult. I think I can trigger another controller method that gets role data for the user with another JS code but I think it is not a good way to do this.
I am a begginer so if the solution is easy please don't blame me... I hope I made myself clear. Any help would be appreciated.