ASP.NET MVC-将搜索条件作为输入,并在同一视图中显示结果 [英] ASP.NET MVC - Taking search criteria as input, and displaying the results, in the same View

查看:139
本文介绍了ASP.NET MVC-将搜索条件作为输入,并在同一视图中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个ASP.NET Razor视图中,我想显示10个文本框(用户将在其中输入各种搜索条件),然后当用户提交表单并且服务器处理该动作时,我想显示结果显示在同一视图中(假设需要显示约1,000行).除了结果,我还希望使用用户以前选择的值填充条件文本框(以便用户可以根据需要修改条件,然后再次单击提交".)

In a single ASP.NET Razor view, I want to display 10 textboxes (the user will enter various search criteria into these), and then when the user submits the form and the server processes the action, I want to display the results in the same view (let's assume that there can be around a 1,000 rows that need to be displayed). Along with the results, I also want criteria textboxes populated with the values that the user had chosen previously (so that the user can modify the criteria if required and then click Submit again).

如何来回传递各种数据?

How do I pass the various data back and forth?

选项1-创建一个单一的视图模型类,该类具有10个属性(每个条件一个)和一个保存结果的第11个属性(一个List). 据我了解,当用户单击提交"时,模型中的值将传递回服务器(这是服务器获取标准值的方式).这是否意味着所有1000行也都传回了?

Option 1 - create a single view model class, that has 10 properties (one for each criteria), and a 11th property (a List) that holds the results. As far as I understand, the values in the model will be passed back to the server when the user clicks Submit (that's how the server gets the criteria values). Does this mean that all the 1,000 rows are also passed back?

选项2-创建一个包含10个属性的视图模型类.使用ViewBag传递保存结果的列表.因此,当用户单击提交第二项"时,仅标准数据被发回,而显示的行不会被发送回服务器(因为它们没有存储在模型中).

Option 2 - create a view model class that holds the 10 properties. Use ViewBag for passing the List that holds the results. Thus, only the criteria data is sent back when the user clicks Submit a 2nd item, while the displayed rows aren't sent back to the server (since they weren't stored in the model).

还有其他方法吗?我应该使用哪种方法?

Are there other approaches? Which approach should I use?

推荐答案

选择选项1:在这种情况下,我没有看到使用ViewBag的任何理由.只需将模型传递到控制器操作方法中的视图即可.

Go with Option 1: I don't see any reason to use ViewBag in this scenario. Just pass the model to the view in your controller action method.

return View(model);

您将有两个操作方法,它们都返回相同的视图.首次访问该页面时的一种方法:

You'd have two action methods, both returning same view. One method for when you first visit the page:

[HttpGet]
public ActionResult Query()
{
    return View(new FormQueryModel())
}

...以及当用户提交搜索条件时的另一种.这将运行查询并传递一个填充有结果的模型,以供视图显示它们.

...and another one for when user submits search criteria. This runs the query and pass a model populated with results for the view to display them.

[HttpPost]
public ActionResult Query(FormQueryModel model)
{
    var queryManager = new QueryManager(model);
    model.QueryResults = queryManager.GetResults();
    return View(model);
}

不,您不必将先前搜索的结果重新发布回控制器.就我而言,我只是将结果保留在form标记之外,因此不会将其回发.但是只要您不绑定结果,就可以了.

And no, you don't have to post back the results from a previous search to the controller again. In my case I just leave results outside the form tag so it's not posted back. But as long you don't bind the result, you'd be okay.

@model FormQueryModel
@using (Html.BeginForm("Query", "Home"))
{
    @Html.LabelFor(m => m.Age)
    @Html.TextBoxFor(m => m.Age)
    @Html.LabelFor(m => m.Country)
    @Html.TextBoxFor(m => m.Country)
}

@if (Model.QueryResults.Count > 0)
{
    @foreach (var result in Model.QueryResults)
    {
       //display results here
    }
}

您还需要添加一些分页,因为用户将不会读取1000行.如果搜索返回的行太多,则用户将添加更多过滤条件.

Also you need to add some pagination as the user is not going to read 1000 rows. If the search is returning too many rows user would add more filter conditions instead.

public class FormQueryModel
{
    public int PageSize { get; set; }

    [Display(Name = "Enter your age")]
    public int Age { get; set; }

    [Display(Name = "Enter your country")]
    public string Country { get; set; }

    public List<QueryResult> QueryResults { get; set; }

    public FormQueryModel()
    {
        this.QueryResults = new List<QueryResult>();
    }
}

这篇关于ASP.NET MVC-将搜索条件作为输入,并在同一视图中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆