0

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.

4

2 回答 2

0

我意识到了问题!数据库的每个条目都有一个以毫秒为单位的 DateTime 对象,而查询则没有。我做了一个解决方法来从所有条目中删除毫秒并且它有效!

于 2013-02-07T03:36:45.920 回答
0

Find()函数从集合中返回匹配项的第一次出现,这将是与条件匹配的单个项。由于您的视图是强类型的IEnumerable<LifestyleGuide.Models.Run>,因此它期望运行对象的集合而不是单个对象。您应该修改控制器,以便将 Run 对象的集合(列表)传递给视图。你可以使用WHERE这样的子句:

public ActionResult Details(String id, DateTime date)
    {
        using (var db = new inspireEntities())
        {
            List<Run> run = db.Runs.Where(r=>r.Id==id && r=>r.Date==date);
            return View(run);
        }
    }

祝你好运 :)。

于 2013-02-06T00:18:12.970 回答