Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anton 5 posts 95 karma points
    Jan 11, 2016 @ 10:38
    Anton
    0

    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:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web
    @using Cockpit.Controllers
    @{
        Layout = "Master.cshtml";
    }
    
    @using (Html.BeginUmbracoForm<MgmtApiSurfaceController>("GetEngagements"))
    {   
        <table cellpadding="2" cellspacing="2" border="1">
            <tr>            
                <th>Engagement Name</th>
                <th>PSP</th>
                <th>Resource Group Name</th>            
            </tr>
            @foreach (var e in ViewBag.result)
            {
                <tr>
                    <td>@e.EngagementName</td>
                    <td>@e.Psp</td>
                    <td>@e.ResourceGroupName</td>
                </tr>               
            }
        </table>    
    }
    

    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:

    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?

    Thx, Anton

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jan 11, 2016 @ 11:07
    Alex Skrypnyk
    0

    Hi Anton,

    Are you submitting your form with js ?

    Try to use

    TempData.Add("CustomMessage", "message");
    

    Thanks, Alex

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Jan 11, 2016 @ 11:29
    Nik
    100

    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:

    @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.

  • Anton 5 posts 95 karma points
    Jan 12, 2016 @ 17:00
    Anton
    0

    Hi Nik,

    your recommendation did the job!

    Thank you, Anton

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Jan 12, 2016 @ 17:07
    Nik
    0

    No problem Anton :-)

Please Sign in or register to post replies

Write your reply to:

Draft