简体   繁体   English

在View中使用C# MVC多个动态模型

[英]Using C# MVC multiple dynamic models in View

I have a View with several form that I'm using for searching and displaying the results as partial View in like SearchByNumber, SearchByVehicle, etc.我有一个视图,其中包含多个表单,用于搜索并将结果显示为部分视图,例如 SearchByNumber、SearchByVehicle 等。

I'm trying to load view and execute search for different forms by posting link with querystring like www.example.com/Search?number=101010 from different view.我正在尝试通过从不同的视图发布带有查询字符串(如 www.example.com/Search?number=101010)的链接来加载视图并执行对不同 forms 的搜索。

For the first form, SearchByNumber I only have one parameter, string number and i'm returning view with dynamic Model and its working like it should, but I only manage to make search for this form.对于第一个表单,SearchByNumber,我只有一个参数,字符串编号,我正在返回动态 Model 的视图,它的工作方式应该如此,但我只设法搜索此表单。

Here is my controller:这是我的 controller:

public ActionResult Index(string number)
{
  return View(model: number);
}

and in the View I have:在视图中我有:

<form id="searchbynumberform">
    Search By Any Number:
    <div class="input-group input-group-sm">
        <input type="text" class="form-control" name="number" id="number" value="@Model">
        <span class="input-group-btn">
            <button class="btn btn-primary" type="button" name="numbersearch" id="numbersearch" disabled>
                Search
            </button>
        </span>
    </div>
</form>

My Question is, if anyone can help me, How to perform search let's say on the second form where I have int type and string name parameters?我的问题是,如果有人可以帮助我,如何执行搜索让我们说在我有 int 类型和字符串名称参数的第二种形式上?

Thank You in advance...先感谢您...

At the moment your Model is only the search string that was entered , which seems rather incomplete.目前您的Model只是输入的搜索字符串,这似乎相当不完整。 It would make a lot more sense if the Model also contained the actual search results , which after all is what the user wants to see.如果Model也包含实际的搜索结果会更有意义,毕竟这是用户想要看到的。 And then you can also add the other search properties.然后您还可以添加其他搜索属性。

The MVC approach for this is to create a (View)Model class, somewhere in your project, something like this:对此的 MVC 方法是在项目的某处创建一个(视图)模型 class,如下所示:

public class SearchModel
{
    public string Number { get; set; }
    public int? Type { get; set; }
    public string Name { get; set; }
    public List<SearchResult> SearchResults { get; set; }
}

And then use it eg like this:然后像这样使用它:

public ActionResult Index(string number)
{ 
    var model = new SearchModel
    {
        Number = number,
        SearchResults = GetByNumber(number)
    };
    return View(model);
}

public ActionResult IndexOther(int type, int name)
{
    var model = new SearchModel
    {
        Type = type,
        Name = name,
        SearchResults = GetByTypeAndName(type, name)
    };
    return View(model);
}

And in your Index.cshtml :在您的Index.cshtml中:

@model SearchModel

@* You can now use Model.Number, Model.Type, Model.Name and Model.SearchResults. *@

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

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