[英]Filtering by parameter & Routing
我有一個MachineInfo視圖頁面,該頁面顯示相關機器的60種不同規格,例如處理器信息,內存信息,磁盤信息,數據庫信息等。
此頁面的ActionLink為:
@Html.ActionLink("Machine Info", "MachineInfo", new { id = Model.LicenseKey }) |
控制器 :
public ActionResult MachineInfo(string LicenseKey)
{
if (LicenseKey == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Farm farm = db.Farms.Find(LicenseKey);
if (farm == null)
{
return HttpNotFound();
}
return View(farm);
}
農場模型:
public partial class Farm
{
public Farm()
{
this.FarmChanges = new HashSet<FarmChange>();
this.FarmDetails = new HashSet<FarmDetail>();
this.FarmTables = new HashSet<FarmTable>();
}
public int Id { get; set; }
public string LicenseKey { get; set; }
public string Name { get; set; }
public string CustomerName { get; set; }
public virtual ICollection<FarmChange> FarmChanges { get; set; }
public virtual ICollection<FarmDetail> FarmDetails { get; set; }
public virtual ICollection<FarmTable> FarmTables { get; set; }
}
FarmDetails型號:
public partial class FarmDetail
{
public System.Guid Id { get; set; }
public int FarmId { get; set; }
public int Type { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual Farm Farm { get; set; }
}
所有MachineInfo都來自FarmDetails表中的“值”。
視圖:
@model IEnumerable<FarmManagement.Models.FarmDetail>
@{
ViewBag.Title = "Machine Info";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Machine Info</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Value)
</th>
<th>
@Html.DisplayNameFor(model => model.Farm.LicenseKey)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value)
</td>
<td>
@Html.DisplayFor(modelItem => item.Farm.LicenseKey)
</td>
<td>
</td>
</tr>
}
</table>
@Html.ActionLink("Back to Farms List", "Index")
我正在嘗試使用以下URL顯示特定計算機(LicenseKey = ABXYZ-XYZAB)的MachineInfo: mydomain.com/MachineInfo/ABXYZ-XYZAB
我需要通過LicenseKey 過濾視圖。
經過所有嘗試,我只得到400-錯誤請求錯誤(由於LicenseKey == null)或得到所有計算機的MachineInfo,而不是具有LicenseKey = ABXYZ-XYZAB的特定計算機。
我做錯了什么?
更改您的動作鏈接代碼
@Html.ActionLink("Machine Info", "MachineInfo", new { id = Model.LicenseKey })
至
@Html.ActionLink("Machine Info", "MachineInfo", new { LicenseKey = Model.LicenseKey })
由於動作鏈接參數名稱應與控制器動作參數名稱匹配。
進行以下更改后解決了該問題:
更改后的新控制器:
public ActionResult MachineInfo(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
FarmDetail farmdetail = db.FarmDetails.Where(x => x.FarmId == id).FirstOrDefault();
if (farmdetail == null)
{
return HttpNotFound();
}
return View(farmdetail);
}
更改后的新視圖:
@model FarmManagement.Models.FarmDetail
@{
ViewBag.Title = "Machine Info";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Machine Info</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Type)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Value)
</th>
<th>
@Html.DisplayNameFor(model => model.Farm.LicenseKey)
</th>
<th></th>
</tr>
@for (int i = 0; i < Model.Farm.FarmDetails.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(model => model.Farm.FarmDetails.ElementAt(i).Type)
</td>
<td>
@Html.DisplayFor(model => model.Farm.FarmDetails.ElementAt(i).Name)
</td>
<td>
@Html.DisplayFor(model => model.Farm.FarmDetails.ElementAt(i).Value)
</td>
<td>
@Html.DisplayFor(model => model.Farm.LicenseKey)
</td>
<td>
</tr>
}
</table>
@Html.ActionLink("Back to Farms List", "Index")
我需要在Controller中使用where語句;
FarmDetail farmdetail = db.FarmDetails.Where(x => x.FarmId == id).FirstOrDefault();
並從視圖中刪除IEnumerable ,並使用ElementAt(i)將@foreach更改為@for 。
@Html.DisplayFor(model => model.Farm.FarmDetails.ElementAt(i).Name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.