I have been trying to create a simple portal to track logs from a mobile application. As such, I have used entity framework and MVC3 to help me with this. However recently I have been stuck when trying to retrieve the entity from the database.
Here is the Run class:
namespace LifestyleGuide.Models
{
[DataContract(IsReference = true)]
[KnownType(typeof(User))]
public partial class Run
{
[DataMember]
public string User_ID { get; set; }
[DataMember]
public System.DateTime Date { get; set; }
[DataMember]
public Nullable<int> Distance { get; set; }
[DataMember]
public Nullable<int> Duration { get; set; }
[DataMember]
public Nullable<int> Calories { get; set; }
[DataMember]
public virtual User User { get; set; }
}
}
User_ID and date are form a composite key where User_ID is a foreign key from the User table.
And the following is the "details" method from the controller:
public ActionResult Details(String id, DateTime date)
{
using (var db = new inspireEntities())
{
Run run = db.Runs.Find(id, date);
return View(run);
}
}
However, when i try to run it, the run object always appears as a null.
Any suggestions are greatly appreciated!
EDIT: Here are is the view for the homepage.
@model IEnumerable<LifestyleGuide.Models.Run>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
<center>User ID</center>
</th>
<th>
<center>Date</center>
</th>
<th>
<center>Distance</center>
</th>
<th>
<center>Duration</center>
</th>
<th>
<center>Calories</center>
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.User_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Distance)
</td>
<td>
@Html.DisplayFor(modelItem => item.Duration)
</td>
<td>
@Html.DisplayFor(modelItem => item.Calories)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.User_ID, date = item.Date }) |
@Html.ActionLink("Details", "Details", new { id = item.User_ID, date = item.Date }) |
@Html.ActionLink("Delete", "Delete", new { id = item.User_ID, date = item.Date })
</td>
</tr>
}
</table>
The fields in the table are populated from the database directly and therefore should already exist.
Note: I have no problems with creating and accessing the run objects by using the view. The null run object only occurs when I directly do an insert statement into the database and when i try to retrieve it afterwards using the .find method as shown above.