Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm fairly new to MVC (On and Off user) and I'm struggling to understand how to get the data from the Model into the View.

In my controller I have method which runs the following code:

     var jsonClient = new WebClient();
    jsonClient.Credentials = new NetworkCredential(ApiUsername.apiUsername, ApiPassword.apiPassword);
    jsonClient.Headers.Set("Content-Type", "application/json");
    jsonClient.Headers.Set("Accept", "application/json");
    //Engine Info
    var radWatchList = jsonClient.DownloadString("http://APIURL:XXXX/api/watchlists/Radiology/alerts/active");
    var radAlertsToModel = JsonConvert.DeserializeObject<DashboardModel>(radWatchList).Data;

    return PartialView("~/Views/Radiology/RadPartialViews/RadiologyAlerts.cshtml");
}


Here is my model class

namespace Rhapsody_DashboardV6.Models
{
    public class DashboardModel
    {
        [JsonProperty("data")]
        public Data Data { get; set; }

        [JsonProperty("error")]
        public object Error { get; set; }
    }

    public class Data
    {
        [JsonProperty("alertCount")]
        public long AlertCount { get; set; }

        [JsonProperty("highestSeverity")]
        public string HighestSeverity { get; set; }

        [JsonProperty("alerts")]
        public Alert[] Alerts { get; set; }
    }

    public partial class Alert
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("alertName")]
        public string AlertName { get; set; }

        [JsonProperty("alertMnemonic")]
        public string AlertMnemonic { get; set; }

        [JsonProperty("alertDescription")]
        public string AlertDescription { get; set; }

        [JsonProperty("timestamp")]
        public string Timestamp { get; set; }

        [JsonProperty("state")]
        public string State { get; set; }

        [JsonProperty("severity")]
        public string Severity { get; set; }

        [JsonProperty("componentInfo")]
        public ComponentInfo ComponentInfo { get; set; }

        [JsonProperty("parameters")]
        public Parameter[] Parameters { get; set; }
    }

    public partial class ComponentInfo
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("folderPath")]
        public string FolderPath { get; set; }
    }

    public partial class Parameter
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("displayName")]
        public string DisplayName { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }
    }

}


Now if I place a break point on the following piece of code:

var radAlertsToModel = JsonConvert.DeserializeObject<DashboardModel>(radWatchList).Data;


I can see that it has correctly populated the model properties correctly with all the relevant API responses. At this point, I would originally have used a ViewBag which works perfectly, however, from reading up, Viewbag appears to be fairly poorly received by the MVC community and that I should be looking to do this through a ViewModal class.

I want to pass this data to a Partial View as I have an AJAX script running which refreshes the Partial View every 30 seconds so that it updates the view with any new data from the API.

This is the code in the partial view:


@model IEnumerable<Rhapsody_DashboardV6.Models.Alert>
<div class="radAlertsMainPanel">
    <div class="panel panel-default radAlertPanel">
        <div class="panel-body">
            <label>Active Alerts: </label>
            <div>
                @foreach (var item in Model)
                {
                    @Html.DisplayFor(modelItem => item.Id);
                    @Html.DisplayFor(modelItem => item.AlertDescription);
                    @Html.DisplayFor(modelItem => item.AlertMnemonic);
                    @Html.DisplayFor(modelItem => item.AlertName);
                }
            </div>
        </div>
    </div>
</div>


And this is the View the partial view will be "embedded" on:

@{
    ViewBag.Title = "Radiology Dashboard";
}

@model Rhapsody_DashboardV6.Models.Alert



<div id="radDashboard" class="radDashboard">
    <div id="radTitle" class="radTitle">
        <h1>Radiology Dashboard class="fas fa-x-ray"></h1>
    </div>
</div>

<div class="radIntStatDiv">
    <div id="radIntStat" class="radIntTitle">
        <h4>Interface Status</h4>
    </div>
</div>

<div class="grid">
    <span class="grid-item" id="TEST1"></span>
    <span class="grid-item" id="TEST2"></span>
    <span class="grid-item" id="TEST3"></span>
    <span class="grid-item" id="TEST4"></span>
    <div>

    </div>

</div>
<div class="radAlertsDiv">
    <div id="radAlerts">
        <h4>Radiology Alerts</h4>
    </div>
</div>

<div class="radAlerts">
    <div>
        <div id="RadiologyAlerts"></div>
    </div>
</div>


<div>
    <div>
        <div id="RadiologyInterfaceStatistics"></div>
    </div>
</div>
@Scripts.Render("~/Content/JavaScript/Radiology/radiologyPartialViews.js")


Within the console within Google Chrome it simply states a 500 error without much more information for this partial view.

I'm unsure what next steps I need to take in order to get this data into the Partial View.


If someone could point me in the right direction, or give me some tips in what I'm doing wrong It would be appreciated.

Many Thanks!

What I have tried:

I have looked at a number of StackOverflow pages, and also been following the MVC tutorial on Microsoft, but have been unsuccessful.
Posted
Updated 26-Feb-19 3:14am
v2
Comments
Richard Deeming 26-Feb-19 10:51am    
Try moving the @model directive to the top of the partial view:
@model Rhapsody_DashboardV6.Models.Alert
@{
    ViewBag.Title = "Radiology Dashboard";
}
...


If it still doesn't work, examine the request in the network panel within Chrome's dev tools, and look at the response. If you're testing on your local machine, you should get a detailed error message which will tell you what the problem is.

1 solution

You pass a model to a partial view the same as you do a non-partial view

return PartialView("~/Views/Radiology/RadPartialViews/RadiologyAlerts.cshtml", radAlertsToModel);


You would also need to change the "@model" attribute on the partial view to be DashboardModel and change the view accordingly also.
 
Share this answer
 
Comments
JoshWigley 26-Feb-19 9:33am    
Hi Sitecore,

Thanks for the suggestion, I have made the relevant alterations.

I had originally attempted this with different interations of the @model statement but all I appear to get is an error message of:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Which then, unsurprisingly, doesn't load up the partial view.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900