我有一個問題,如何通過外鍵在 details.cshtml 中獲取類別名稱 - 作業模型中的 CategoryId?
作業.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace InfoKiosk.Domain.Entities
{
public class Work
{
public int WorkId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
}
類別.cs
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
我從下拉串列中選擇的類別名稱是我從 ViewBag 填充的類別。
詳情.cshtml
<dt>
<strong>@Html.DisplayNameFor(model => model.Category):</strong> @Html.DisplayFor(model => model.???)
</dt>
uj5u.com熱心網友回復:
您可以將其傳遞ViewBag給視圖,可能來自Controller:
string NameCategory = from c in context.Category
where c.CategoryId == work.categoryId
select c.Name
uj5u.com熱心網友回復:
用這個
<dt>
<strong>@Html.DisplayNameFor(model => model.Category.Name):</strong> @Html.DisplayFor(model => model.Category.Name)
</dt>
在你的行動中包括類別
var model=context.Set<Work>().Iclude(c=>c.Category).FirstOrDefault(w=> w.WorkId==id);
return View(model);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/338335.html
標籤:C# html css asp.net asp.net-mvc
