简体   繁体   中英

Filtering by parameter & Routing

I have an MachineInfo view page which I am showing 60 different specifications of the related machine like processor info, ram info, disk info, db info etc.

ActionLink to this page is:

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

Controller :

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

Farm Model:

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 Model:

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; }
}

All the MachineInfo is coming from the "Value" in the FarmDetails table.

View:

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

I am trying to show MachineInfo of a specific machine (LicenseKey=ABXYZ-XYZAB) with this url: mydomain.com/MachineInfo/ABXYZ-XYZAB

I need to filter the view by LicenseKey .

After my all tries, I'm only getting 400 - Bad Request error (Because LicenseKey == null) or getting the MachineInfo of ALL machines, not the specific machine with LicenseKey=ABXYZ-XYZAB.

What I am doing wrong?

Change your actionlink code

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

to

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

As the Action link parameter name should match with the controller action parameter name.

Solved the problem after making following changes:

New Controller After Change:

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

New View After Change:

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

I needed to use a where sentence in Controller;

FarmDetail farmdetail = db.FarmDetails.Where(x => x.FarmId == id).FirstOrDefault();

And removed IEnumerable from the View and changed @foreach to @for with ElementAt(i) .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM