简体   繁体   English

为什么从GET操作填充的唯一隐藏字段没有在模型中传递?

[英]Why the only hidden field that is being filled from GET action is not being passed in model?

Sorry for the long title, I didn't know how to make it any shorter. 对不起,长标题了,我不知道如何缩短标题。

  • My code: 我的代码:

My model: 我的模特:

public class CarFilter {
    public String carMake { get; set; }
    public String carModel { get; set; }
    public String carEdition { get; set; }
    .
    .
    .
    public String SortBy { get; set; }
}

public class CarSearch : CarFilter {
    public List<Car> Car { get; set; }
}

My controller: 我的控制器:

public ActionResult SearchResult(CarSearch search)
    {
        var cars = from d in db.Cars
                   select d;

        if (Request.HttpMethod == "POST")
        {
            search.SortBy = "Price";
        }
        search.Car = new List<Car>();
        search.Car.AddRange(cars);

        var temp = new List<CarSearch>();
        temp.Add(search);

        return View(temp);
}

My Index view (where user filters results): 我的索引视图(用户在其中过滤结果):

@model IEnumerable<Cars.Models.CarSearch>
@using (Html.BeginForm("SearchResult", "Home", FormMethod.Post)){..forms and other stuff..}

My SearchResult view (where user sees the results of filtration): 我的SearchResult视图(用户可以在其中查看过滤结果):

@model IEnumerable<Cars.Models.CarSearch>
@using (Html.BeginForm("SearchResult", "Home", FormMethod.Get))
{
@Html.Hidden("carMake")
@Html.Hidden("carModel")
@Html.Hidden("carEdition")
.
.
.
@Html.Hidden("SortBy")

<input name="SortBy" class="buttons" type="submit" value="Make"/>
  • My goal 我的目标

What I'm trying to do is when user clicked on sort by Make it will have to GET back all the variables in hidden field back to the SearchResult action in order to sort the result (same filtered results). 我要尝试做的是,当用户单击“排序”时,必须将隐藏字段中的所有变量取回SearchResult操作,以便对结果进行排序(相同的过滤结果)。

  • Result 结果

Is: <input id="SortBy" name="SortBy" type="hidden" value=""/> . 是: <input id="SortBy" name="SortBy" type="hidden" value=""/> The value is null and it's not being passed but all the other hidden fields such as carMake and etc have value. 该值为null,并且不会传递,但是所有其他隐藏字段(例如carMake等)都具有值。 But when I use foreach it works perfect. 但是,当我使用foreach它可以完美工作。

  • Question

Why is this like this? 为什么这样呢? the SortBy is in the same model class as other fields in the view. SortBy与视图中的其他字段位于同一模型类中。 The only difference is that SortBy is not being filled in the Index view with other fields, instead it's being filled in controller action. 唯一的区别是,SortBy不会在索引视图中与其他字段一起填充,而是在控制器操作中填充。 What is the explanation for this? 这有什么解释? Am I missing any C# definition or something such as dynamic objects or something? 我是否缺少任何C#定义或诸如动态对象之类的东西?

You are not binding any of the html elements to anything in your model, you are just telling it to create hidden fields with specific names. 您没有将任何html元素绑定到模型中的任何内容,而只是告诉它创建具有特定名称的隐藏字段。

Instead of using @Html.Hidden("carMake") try @Html.HiddenFor(model => model.CarMake) . 而不是使用@Html.Hidden("carMake")尝试@Html.HiddenFor(model => model.CarMake)

Now, you are going to need to send your search settings to the view as well somehow. 现在,您将需要以某种方式将搜索设置发送到视图。 The inheritance you have on your ViewModels is throwing me off a bit. 您对ViewModels的继承使我有点失望。 I don't think there's much of a reason for having CarSearch inherit from CarFilter . 我认为让CarSearch继承自CarFilter的理由CarFilter Is a search really a filter? 一个搜索真的是一个过滤器? It may be clearer to have the Action SearchResult receive a CarFilter , and return a CarSearch , where CarSearch could be: 让Action SearchResult接收CarFilter并返回CarSearch可能更清楚,其中CarSearch可以是:

public class CarSearch 
{
    public CarFilter Filter { get; set; }
    public List<Car> Cars { get; set; }
}

And the action would look like: 动作看起来像:

public ActionResult SearchResult(CarFilter filter)
{
    ...
    return View(new CarSearch {Filter = filter, Cars = <your result>});
}

That way your action is more clear regarding what is input and what is output. 这样,您在输入什么和输出什么方面的操作就更加清楚了。 With that model your hidden fields would be something like: 使用该模型,您的隐藏字段将类似于:

@Html.HiddenFor(model => model.Filter.CarMake)

And for the actual Cars that represent the search result, you could either do a foreach , a partial view, or a Display template using @Html.DisplayFor() . 对于代表搜索结果的实际@Html.DisplayFor() ,您可以使用@Html.DisplayFor()进行foreach ,局部视图或Display模板。 I'd probably go with the last one. 我可能会选择最后一个。

Note : I changed the casing on your properties since you probably want to stick to the C# naming conventions. 注意 :我更改了属性的大小写,因为您可能想遵循C#命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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