在我的應用程式中,我使用了 Entity Framework 6 和 C# 的 ASP.NET MVC。
我有一個擁有記錄的表,我打算用它來填充我的索引頁。我如何在不使系統將記錄的id添加到URL中的情況下填充索引頁。請看下面的例子。我已經研究過路由問題,但在添加自定義路由時,你不得不在URL中添加更多的文字,而我只想讓URL顯示為example.com。我不希望也不需要example.com/MenuRecords/Details/20讓用戶看到。
所以example.com應該在HomeController的索引視圖中從下面的模型加載以下資料。
index.cshtml頁面呼叫下面顯示的模型資料:
@model example.Models.tblMenuRecords
@Model.ThisWeeksBestDrink
@Model.ThisWeeksBestAppetizer
@Model.ThisWeeksBestDesert
@Model.ThisWeeksBestLunchSpecial
這是cntroller的動作方法:
這是cntroller的動作方法。
public ActionResult Index()
{
returnView()。
}
我怎樣才能讓它在Index頁面正常作業?由于這是一個從模型中呼叫資料的主頁,我不能讓 URL 具有 example.com .... 以外的任何內容,但我知道當從模型中呼叫資料時,你確實需要某種 ID,但我真的不明白如何做到這一點。
我知道有一個路由配置包括這個默認路由,它允許你只顯示域名......但是當你試圖從資料庫加載資料時,如何做到這一點呢?
routes. MapRoute("Default"/span>, "{controller}/{action}/{id}"/span>, new { controller = "Home", action = "index", id = UrlParameter. 可選}
這是否是向視圖傳遞tblMenuRecords實體的正確方式?
public ActionResult Index()
{
tblMenuRecords tblMenuRecords = db.tblMenuRecords()。
return View(tblMenuRecords)。
uj5u.com熱心網友回復:
我認為你的視圖是錯過了with模型。視圖代碼應如下所示
@model Models.MenuRecords
@{
ViewBag.Title = "選單記錄"。
}
<h2>細節</h2>
<div>
<h4>選單</h4>
<hr />
<table>
<tr>
<td>
@Html.DisplayFor(model => model.ThisWeeksBestDrink)
</td>
<td>
@Html.DisplayFor(model => model.ThisWeeksBestLunchSpecial)
</td>
</tr>
</table>
</div>
我希望,它能幫助你。
uj5u.com熱心網友回復:
我認為你必須修復這個動作
public ActionResult Index()
{
tblMenuRecords tblMenuRecords = db.tblMenuRecords.FirstOrDefault()。
return View(tblMenuRecords)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/324070.html
標籤:
