这是索引视图中的ActionLink。
@Html.ActionLink("€ 50-100", "Filter", new { number1 = 50, number2 = 100 }, null)
以下是控制器中用于捕获2个参数的过滤方法
[HttpGet]
public ActionResult Filter(int number1, int number2)
{
var result = db.Gifts.Where(c => c.Price > number1 && c.Price <= number2).ToList();
return View(result);
}
一切正常,但我想在过滤器视图中使用参数forexample;
<h3> Choose a gift from the list of € number1 - number2 </h3>
我该怎么做?
答案 0 :(得分:1)
包含过滤器参数和结果集的新ViewModel:
public class MyViewModel
{
public int Number1 { get; set; }
public int Number2 { get; set; }
public List<Gifts> GiftList { get; set; }
}
更新了操作方法。您可以在此处指定模型属性并将模型传递给视图:
public ActionResult Filter(int number1, int number2)
{
var model = new MyViewModel
{
Number1 = number1,
Number2 = number2,
GiftList = db.Gifts.Where(c => c.Price > number1 && c.Price <= number2).ToList()
}
return View(model);
}
更新了视图。您需要更改顶部的模型声明以使用新模型:
@model MyViewModel
<h3> Choose a gift from the list of € @Model.Number1 - @Model.Number2 </h3>
您将迭代Gifts
结果集,如下所示:
@foreach(var item in Model.GiftList)
{
...
答案 1 :(得分:-1)
根据您的代码,您似乎试图将某些值从控制器传递到视图。
您可以使用ViewBag执行此操作: 控制器:
subjectList
查看:
@ Html.ActionLink(“€50-100”,“过滤器”,新{number1 = @ ViewBag.N1,number2 = @ ViewBag.N2},null)
或者使用模型:
控制器:
public ActionResult Filter(int number1, int number2)
{
var result = db.Gifts.Where(c => c.Price > number1 && c.Price <= number2).ToList();
ViewBag.N1 = 50;
ViewBag.N2 = 100;
return View();
}
查看:
public class MyModel
{
public Int32 N1 {get;set;}
public Int32 N2 {get;set;}
}
public ActionResult Filter(int number1, int number2)
{
var result = db.Gifts.Where(c => c.Price > number1 && c.Price <= number2).ToList();
return View(new MyModel{N1 = 50, N2 = 100});
}