Reputation: 33
Hey im sure this is a simple question but im completely new to MVC EF . I have a view that takes 3 inputs . 2 are Dates and the other one is the ID of the roller-coaster ride which is called "ID". If all the inputs are filled it gives you the number of guest that Rode that specific Ride . If the ID is NULL then it gives out the most popular Ride and the number of guest that rode that specific ride. My issue is how to get the right sentence to appear. Right now the sentence says "The number of guests in the given date range was" but the problem is that it says the same thing when it is null . So it says "The number of guests in the given date range 5 7" . Where 5 is the most popular Ride and 7 is the number of guest. i rather have the sentence say "the most popular ride was 5 and it had 7 guest" . So basically what im asking is if there is some type of If-else statement i can use in the view to get the right sentence to appear? Below is my code for the view and controller
@model IEnumerable<ThemeParkManagementSystem.Models.RideCount>
@{
ViewBag.Title = "Index";
}
<h2>Reports</h2>
<p>
@using (Html.BeginForm("Index", "Reports", FormMethod.Get))
{
<text> Date 1 </text>
@Html.TextBox("date1")
<text> Date 2 </text>
@Html.TextBox("date2")
<text> Ride ID </text>
@Html.TextBox("id")<input type="submit" value="Search" />
}
</p>
<table>
<tr>
<th>
<text>The number of guests in the given date range for </text> @ViewData["RideID"] @ViewData["GuestCount"]
</th>
</tr>
</table>
private tpdatabaseEntities db = new tpdatabaseEntities();
public ActionResult Index(DateTime? date1, DateTime? date2, int? id)
{
if (id == null)
{
Nullable<int> popularRideID = db.topRides(date1, date2).ToList<Nullable<int>>().FirstOrDefault();
var popularID = popularRideID;
DateTime? d1 = date1;
DateTime? d2 = date2;
Nullable<int> numOfGuest = db.Guestcount(d1, d2, popularID).ToList<Nullable<int>>().FirstOrDefault();
var guestCount = numOfGuest.Value;
ViewData["RideID"] = popularID;
ViewData["GuestCount"] = numOfGuest;
return View();
}
else
{
Nullable<int> countList = db.RideCount(date1, date2, id).ToList<Nullable<int>>().FirstOrDefault();
var rideCount = countList.Value;
ViewData["RideCount"] = rideCount;
}
return View();
}
Upvotes: 0
Views: 65
Reputation: 159
I agree with @Steve in the comments because it is probably wiser to leave that logic in the controller. However, as you are new to MVC EF maybe this solution can help you in another scenario. As you are just returning a single value from your search, there is no need to use an IEnumerable model.
If you use a model of type ThemeParkManagementSystem.Models.RideCount this will give you access to it's values in the view and then you can use a simple if statement as shown in the code below.
Not the most optimal solution, but could come in handy in future scenarios.
@model ThemeParkManagementSystem.Models.RideCount
@{
ViewBag.Title = "Index";
}
<h2>Reports</h2>
<p>
@using (Html.BeginForm("Index", "Reports", FormMethod.Get))
{
<text> Date 1 </text>
@Html.TextBox("date1")
<text> Date 2 </text>
@Html.TextBox("date2")
<text> Ride ID </text>
@Html.TextBox("id")<input type="submit" value="Search" />
}
</p>
<table>
<tr>
<th>
@if (model.id==null){
<text>The number of guests in the given date range for </text> @ViewData["RideID"] @ViewData["GuestCount"]
}
else{
Other text value
}
</th>
</tr>
</table>
Upvotes: 1