按参数&进行过滤路由

时间:2016-02-15 09:29:34

标签: c# asp.net-mvc

我有一个MachineInfo视图页面,我显示了相关机器的60种不同规格,如处理器信息,ram信息,磁盘信息,数据库信息等。

此页面的

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表中的“Value”。

查看:

@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")

我正在尝试使用此网址显示特定计算机的MachineInfo(LicenseKey = ABXYZ-XYZAB): mydomain.com/MachineInfo/ABXYZ-XYZAB

我需要通过 LicenseKey 过滤视图。

在我所有尝试之后,我只得到400 - 错误请求错误(因为LicenseKey == null)或得到所有机器的MachineInfo,而不是具有LicenseKey = ABXYZ-XYZAB的特定机器

我做错了什么?

2 个答案:

答案 0 :(得分:0)

更改您的actionlink代码

 @Html.ActionLink("Machine Info", "MachineInfo", new { id = Model.LicenseKey })

@Html.ActionLink("Machine Info", "MachineInfo", new { LicenseKey = Model.LicenseKey })

由于Action链接参数名称应与控制器操作参数名称匹配。

答案 1 :(得分:0)

进行以下更改后解决了问题:

更改后的新控制器:

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();

从View中删除 IEnumerable ,并使用 ElementAt(i) @foreach 更改为 @for

@Html.DisplayFor(model => model.Farm.FarmDetails.ElementAt(i).Name)