I have an MVC VS2013 project querying an XML document.
The index action on my controller returns a list of Restaurants, and I need to be able to click the individual restaurants and open a details page which will show the respective properties of that Restaurant.
In previous projects querying from a sql database I would just call the db context, for example
Restaurant restaurant = db.Restaurants.Find(id);
but I am uncertain what the XML querying equivalent is.
Here is what I have so far.
Model:
[XmlRoot("EstablishmentCollection")]
public class Restaurant
{
[Key]
[XmlElement("FHRSID")]
public int? FHRSID { get; set; }
[XmlElement("BusinessName")]
public string BusinessName { get; set; }
[XmlElement("RatingValue")]
public int? RatingValue { get; set; }
[XmlElement("Hygiene")]
public int? HygieneScore { get; set; }
}
Controller:
public ActionResult Index()
{
IQueryable<Restaurant> Res = null;
try
{
var qy = xmlDoc.Descendants("EstablishmentDetail").Select(n => new Restaurant()
{
FHRSID = (int)n.Element("FHRSID"),
BusinessName = n.Element("BusinessName").Value,
RatingValue = RValue(n),
HygieneScore = HScores(n)
});
Res = qy.AsQueryable();
}
catch (Exception ex)
{
string message;
message = ex.Message.ToString();
return View(message);
}
return View(Res);
}
public ActionResult Details (int? id)
{
try
{
var qy = xmlDoc.Descendants("EstablishmentDetail").Select(n => new Restaurant()
{
FHRSID = (int)n.Element("FHRSID"),
BusinessName = n.Element("BusinessName").Value,
RatingValue = RValue(n),
HygieneScore = HScores(n)
});
//What goes here???
}
catch (Exception ex)
{
string message;
message = ex.Message.ToString();
return View(message);
}
return View();
}
Details View:
@model WebApplication3.Models.Restaurant
<h2>Details</h2>
<h6>@Model.FHRSID</h6>
<h6>@Model.BusinessName</h6>
<h6>@Model.HygieneScore</h6>
<h6>@Model.RatingValue</h6>
Still a bit new to programming, so any help would be appreciated!
No comments:
Post a Comment