gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:49:40 35 4
gpt4 key购买 nike

我有一个具有多种形式的 View ,用于搜索并将结果显示为部分 View ,如 SearchByNumber、SearchByVehicle 等。

我正在尝试通过从不同的 View 发布带有查询字符串(如 www.example.com/Search?number=101010)的链接来加载 View 并执行对不同表单的搜索。

对于第一种形式,SearchByNumber,我只有一个参数,字符串数字,我正在返回带有动态模型的 View ,它的工作方式应该如此,但我只能设法搜索这种形式。

这是我的 Controller :

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

在我的 View 中:

<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>

我的问题是,如果有人能帮助我,假设在第二种形式上我有 int 类型和字符串名称参数,如何执行搜索?

提前谢谢你...

最佳答案

目前您的模型 只是输入的搜索字符串,这看起来相当不完整。如果 Model 还包含实际的搜索结果,那就更有意义了,毕竟这是用户想要看到的。然后您还可以添加其他搜索属性。

为此,MVC 方法是在项目的某处创建一个 (View)Model 类,如下所示:

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

然后使用它,例如像这样:

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);
}

在你的 Index.cshtml 中:

@model SearchModel

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

关于javascript - 在 View 中使用 C# MVC 多个动态模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523364/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com