ERROR :The model item passed into the ViewDataDictionary is of type 'System.Net.Http.HttpResponseMessage', but this
In my Asp.net core project, I get ERROR :The model item passed into the ViewDataDictionary is of type 'System.Net.Http.HttpResponseMessage', but this ViewDataDictionary instance requires a model item of type MENTOR.Models.Mentor
error. What ı'm trying to do is;
In login controller
I'm sending the login and password data as a parameter to API and all ı need to get mentorId(which property of Mentor) property from API response. Then all ı need to send this id to Profile
action of mentor controller
. Then ı will get this id from the session which comes from login controller
then send it to API and fill the profile model with a response model. The difference of my question from other same questions is I'm not sending and model to mentor controller
from login controller
. All ı need is just trigger the profile
action of mentor controller
Here is my login controller
;
[HttpPost]
public async Task<IActionResult> Login(User user)
{
using (var client = new HttpClient())
{
IList<KeyValuePair<string, string>> userCollection = new List<KeyValuePair<string, string>> {
{ new KeyValuePair<string, string>("email",user.email) },
{ new KeyValuePair<string, string>("password", user.password) }
};
var result = await client.PostAsync("http://localhost:3000/mentor/login", new FormUrlEncodedContent(userCollection));
string resultContent = await result.Content.ReadAsStringAsync();
var response = JsonConvert.DeserializeObject<Mentor>(resultContent);
HttpContext.Session.SetInt32("mentorId", response.mentorId);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
return RedirectToAction("Profile", "Mentor");
}
Here is my mentor controller
;
[HttpGet]
public async Task<IActionResult> Profile()
{
using (var client = new HttpClient())
{
var id = HttpContext.Session.GetInt32("mentorId");
var response = await client.GetAsync("http://localhost:3000/mentor/getProfileInfo/" + id);
string responseContent = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Mentor>(responseContent);
result.mentorId = Convert.ToInt32(id);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return View(response);
}
else
{
return StatusCode(404, result.mentorId);
}
}
}
and my model of profile view
;
@model MENTOR.Models.Mentor
thx in advance :))
Solution 1:
Fix the error - instead of using response use result:
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var result = JsonConvert.DeserializeObject<Mentor>(responseContent);
result.mentorId = Convert.ToInt32(id); //Maybe you don't need it???
return View(result);
}
else
{
return StatusCode(404, id);
}