控制器 :
public ActionResult Index()
{
return View(db.Profits.ToList());
}
public ActionResult Create(Profits profits)
{
{
var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
var category = db.Categories.FirstOrDefault(o => o.Id == profits.CategoryID);
var profit = new Profits
{
Value = profits.Value,
Description = profits.Description,
DateInput = profits.DateInput,
CategoryName = category,
User = user
};
db.Profits.Add(profit);
db.SaveChanges();
return RedirectToAction("Index");
}
}
索引.cshtml:
model IEnumerable<WebHFM.Models.Profits>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateInput)
</td>
在 MSSQL Server 中,我看到了正确的信息。名为列的表利润具有=CategoryName_id
正确的数据(1 或 2 或我的主键 table Categories
),但在我应该看到的索引视图中Name
(类别模型)我到处都只看到数字 0。
基本上在我的View
我想看到一个 HTML 表格,其中列名来自Profits
Model:
Categories
, value
, description
,dataInput