我在學習如何使用部分視圖。我做了一個簡單的示例應用程序,其中需要一些幫助來填充以便能夠理解(並且理解部分視圖的重點),並將所有部分粘合在一起以便能夠運行這個簡單的測試應用程序。我已閱讀了部分視圖的一些示例,但我不明白!需要幫助將代碼粘合在一起
該應用程序僅在用Entity Framework創建的數據庫中顯示First Name
和Last Name
的列表。
感激,如果我能得到一些幫助!
模型的人看起來是這樣的:
namespace RenderActionTest.Models
{
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
局部視圖看起來是這樣的:
@model RenderActionTest.Models.Person
<tr>
<td>
@Html.DisplayFor(modelItem => Model.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => Model.LastName)
</td>
</tr>
索引視圖看起來是這樣的:
@model IEnumerable<RenderActionTest.Models.Person>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
@{
Html.RenderAction(" ?? ", " ?? ");
}
}
</table>
最後控制器看起來像這樣:
// GET: Persons
public ActionResult Index()
{
var person = new Person();
// return View(db.People.ToList());
return PartialView(" ?? ", person);
}
感謝您的信息! –