尝试将多个参数传递给局部视图时出现错误

时间:2018-10-19 21:25:54

标签: asp.net-mvc asp.net-mvc-4

我正在创建花朵过滤器。我们可以按颜色,尺寸或起始价格选择花朵。这是我的过滤器模型

 public class FilterController : Controller
{
    // GET: FilterModel
 private asp6Entities db = new asp6Entities();
    public ActionResult Index()
    {
        FilterModel model = new FilterModel();

        var color = db.COLORs.ToList().Select(s => new SelectListItem
        {
            Text = s.COLOR_NAME,
            Value = s.COLOR_ID.ToString()
        });

        var Size = db.FLOWERs.ToList().Select(s => new SelectListItem
        {
            Text = s.FLOWER_SIZE,
            Value = s.COLOR_ID.ToString()
        });

        var StartPrice = db.FLOWERs.ToList().Select(s => new SelectListItem
        {
            Text = s.FLOWER_PRICE.ToString(),
            Value = s.COLOR_ID.ToString()
        });

        var EndPrice = db.FLOWERs.ToList().Select(s => new SelectListItem
        {
            Text = s.FLOWER_PRICE.ToString(),
            Value = s.COLOR_ID.ToString()
        });

        return PartialView("~/Views/Shared/_FilterForm.cshtml", new FilterModel { AllColorOptions = color}, new FilterModel { AllSizeOptions = Size }, new FilterModel { AllStartingPriceOptions = StartPrice }, new FilterModel { AllEndingPriceOptions = EndPrice });
    }
}

我从返回的局部视图中收到此错误。

  

CS1501 C#方法的无重载采用5个参数

这是家庭控制器:

  [HttpPost]
    public ActionResult Index(FilterModel fromColorFilter)
    {
        int SelectedColor = int.Parse(fromColorFilter.ColorSelected);

        var allFlowers = db.FLOWERs.ToList();
        List<FLOWER> result = new List<FLOWER>();
        foreach (var flower in allFlowers)
        {
            if (flower.COLOR_ID == SelectedColor)
            {
                FLOWER model = new FLOWER();
                model = flower;
                result.Add(model);
            }
        }

        return View(result);
    }

这是我的_FilterForm部分视图。

我正在从数据库中获取选择

<div class="FilterForm">
<form>
    <input type="text" placeholder="Search.." name="search" class="SearchInput">
</form>

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <h3>Filter Products</h3>
    <p>
        Color:
        @Html.DropDownListFor(s => s.ColorSelected, Model.AllColorOptions, "Please Choose a Color")
    </p>

    <p>
        Size:
        @Html.DropDownListFor(s => s.SizeSelected, Model.AllSizeOptions, "Please Choose a Size")
    </p>

    <p>
        Price :
        @Html.DropDownListFor(s => s.StartingPriceSelected, Model.AllStartingPriceOptions, "Please Choose a Size")) @Html.DropDownListFor(s => s.EndingPriceSelected, Model.AllEndingPriceOptions, "Please Choose a Size"))
    </p>

    <input type="submit" value="Filter" style="margin-left: 120px" />
}

这是它使用的模型声明

    public class FilterModel
{
    //declaring the colors selection
    public string ColorSelected { get; set; }

    //Creating the Size selection
    public string SizeSelected { get; set; }

    //Creating the starting price selection
    public int StartingPriceSelected { get; set; }

    //Creating Ends price Selection
    public int EndingPriceSelected { get; set; }


    //creating IEnumerable of all color options
    public IEnumerable<SelectListItem> AllColorOptions { get; set; }

    //creating IEnumerable of all Size Options
    public IEnumerable<SelectListItem> AllSizeOptions { get; set; }

    //creating IEnumerable of Starting Price Options
    public IEnumerable<SelectListItem> AllStartingPriceOptions { get; set; }

    //creating IEnumerable of Ending Price Options
    public IEnumerable<SelectListItem> AllEndingPriceOptions { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您需要创建FilterModel类的一个对象,设置该对象的不同属性,并将FilterModel对象传递到局部视图。

var vm = new FilterModel();

//Set the different collection properties
vm.AllColorOptions = color;
vm.AllStartingPriceOptions = StartPrice;
vm.AllEndingPriceOptions = EndPrice;
vm.AllSizeOptions = Size;

// return the partial view while passing the view model object of the PartialView
return PartialView("~/Views/Shared/_FilterForm.cshtml", vm);

假设您的_FilterForm.cshtml视图被强力键入FilterModel类。