[英]Linq query using select box to display data
我正在嘗試實現一個 linq 查詢,它允許用戶選擇例如顯示在選擇框中的 1-10,並根據用戶單擊的那個顯示相應的“CarID”。
您可以參考以下工作示例:
1.型號
public class Car
{
public int CarID { get; set; }
public string Model { get; set; }
public string Manufacturer { get; set; }
public string EngineSize { get; set; }
}
2.頁面模型:
public class Query1Model : PageModel
{
private readonly RazorPageDbContext _context;
public Query1Model(RazorPageDbContext context)
{
_context = context;
}
public IList<Car> CarID { get; set; }
public List<SelectListItem> Options { get; set; }
[BindProperty(SupportsGet =true)]
public int SearchCarID { get; set; }
public async Task<IActionResult> OnGetAsync()
{
Options = _context.Cars.Select(a =>
new SelectListItem
{
Value = a.CarID.ToString(),
Text = a.CarID.ToString()
}).ToList();
if (SearchCarID != 0)
{
var CarQuery = (from s in _context.Cars
where s.CarID == SearchCarID
select s);
CarID = await CarQuery.ToListAsync();
}
else
{
CarID = await _context.Cars.ToListAsync();
}
return Page();
}
}
3.剃須刀頁面:
<h2>Filter By CarID</h2>
<form>
<p>
Select the CarID you wish to display:
<select asp-for="SearchCarID" asp-items="@Model.Options">
<option value="">Pick one</option>
</select>
<input type="submit" value="Find" />
</p>
</form>
<h3> Here is the CarIDs you requested</h3>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CarID[0].CarID)
</th>
<th>
@Html.DisplayNameFor(model => model.CarID[0].Model)
</th>
<th>
@Html.DisplayNameFor(model => model.CarID[0].Manufacturer)
</th>
<th>
@Html.DisplayNameFor(model => model.CarID[0].EngineSize)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.CarID)
{
<tr>
<td>
@Html.DisplayFor(modelitem => item.CarID)
</td>
<td>
@Html.DisplayFor(modelitem => item.Model)
</td>
<td>
@Html.DisplayFor(modelitem => item.Manufacturer)
</td>
<td>
@Html.DisplayFor(modelitem => item.EngineSize)
</td>
</tr>
}
</tbody>
</table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.