Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 14: Resource Group Name
Line 15:
Line 16: @foreach (var e in ViewBag.result)
Line 17: {
Line 18:
If I debug the application I see that the controller is not called and that is the reason why ViewBag is empty...
Controller:
public class MgmtApiSurfaceController : SurfaceController
{
// GET
public ActionResult GetEngagements()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://xxxxxx.azurewebsites.net"); //
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")
);
HttpResponseMessage response = client.GetAsync("/api/Engagement").Result;
if (response.IsSuccessStatusCode)
{
ViewBag.result = response.Content.ReadAsAsync<IEnumerable<MgmtApiModels.Engagement>>().Result;
}
else
{
ViewBag.result = "Error";
}
return View();
}
}
What could be the reason why the controller is not called?
I have to say your code is a bit confusing. Why are you calling a Get method with Umbraco's Begin Form helper?
This request wouldn't (as far as I understand it) be called until said form was submitted.
If what you are trying to do is render results of an api call I would do something similar to this:
1) Replace your form with:
@Html.Action("GetEngagements", "MgtApiSurface")
2) Have your GetEngagements action render a partial view result which does the drawing of your table.
In said partial view you could either have a typed model or you need to check that ViewBag.result is not null/error before you try to render the results in the table.
SurfaceController not working
Hi All,
what I'm trying to do is to call a WebApi in my custom SurfaceController and render the data in an umbraco template.
Template:
I receive the following error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 14: Resource Group Name
Line 15: Line 16: @foreach (var e in ViewBag.result) Line 17: { Line 18:
If I debug the application I see that the controller is not called and that is the reason why ViewBag is empty...
Controller:
What could be the reason why the controller is not called?
Thx, Anton
Hi Anton,
Are you submitting your form with js ?
Try to use
Thanks, Alex
Hi Anton,
I have to say your code is a bit confusing. Why are you calling a Get method with Umbraco's Begin Form helper?
This request wouldn't (as far as I understand it) be called until said form was submitted.
If what you are trying to do is render results of an api call I would do something similar to this:
1) Replace your form with:
2) Have your GetEngagements action render a partial view result which does the drawing of your table. In said partial view you could either have a typed model or you need to check that ViewBag.result is not null/error before you try to render the results in the table.
Hi Nik,
your recommendation did the job!
Thank you, Anton
No problem Anton :-)
is working on a reply...