Html 向控制器传递大量参数?
我正在创建一个“高级输入表单”,其中包含大量用于搜索数据的输入。我的问题是:将大量数据从HTML传递到控制器的最佳方式是什么 我问的原因是。假设您有以下HTML表单:Html 向控制器传递大量参数?,html,asp.net,asp.net-mvc-4,Html,Asp.net,Asp.net Mvc 4,我正在创建一个“高级输入表单”,其中包含大量用于搜索数据的输入。我的问题是:将大量数据从HTML传递到控制器的最佳方式是什么 我问的原因是。假设您有以下HTML表单: @using (Html.BeginForm("Loading", "AdvancedSearch")) { <input type="text" id="keyword"> <input type="text" id="keyword1"> &l
@using (Html.BeginForm("Loading", "AdvancedSearch"))
{
<input type="text" id="keyword">
<input type="text" id="keyword1">
<input type="text" id="keyword2">
<input type="text" id="keyword3">
<input type="text" id="keyword4">
<input type="text" id="keyword5">
<input type="text" id="keyword6">
<input type="text" id="keyword7">
<input type="text" id="keyword8">
<input type="text" id="keyword9">
<input type="submit" value="Search" style="width: 150px;" />
}
那么你将如何执行这个动作,或者你会这样做
谢谢 使用模型类。如果输入名称与模型属性匹配,MVC引擎将为您进行映射
public class Keywords
{
public string keyword1 { get; set; }
public string keyword2 { get; set; }
///etc...
}
你的行动要简单得多:
public ActionResult Loading(Keywords keywords){
//do things to the parameters!
var keyword1 = keywords.keyword1;
return View();
}
我建议使用一个视图模型,并包含一个关键字列表。为每个关键字添加属性是没有意义的:
public class Keywords
{
public List<string> Items { get; set; }
}
public ActionResult Loading(Keywords keywords){ }
公共类关键字
{
公共列表项{get;set;}
}
公共操作结果加载(关键字){}
或如有可能:
public ActionResult Loading(List<string> keywords){ }
公共操作结果加载(列出关键字){
阅读更多关于它的信息。用这些关键字1、关键字2等创建一个类 比如
public class SearchDto
{
public string Keyword1 { get; set; }
public string Keyword2 { get; set; }
public string Keyword3 { get; set; }
public string Keyword4 { get; set; }
public string Keyword5 { get; set; }
public string Keyword6 { get; set; }
public string Keyword7 { get; set; }
public string Keyword8 { get; set; }
}
然后行动结果如
public ActionResult Loading(SearchDto dto)
{
return View();
}
您可以从视图发布数据
这里有一个例子
还有这里
function search() {
$.ajax({
type: "POST",
url: '@Url.Action("CreateEmail", "Email")',
data: JSON.stringify({
Keyword1 : $("#keyword1").val(),
Keyword2 : $("#keyword2").val(),
Keyword3 : $("#keyword3").val(),
Keyword4 : $("#keyword4").val(),
Keyword5 : $("#keyword5").val(),
Keyword6 : $("#keyword6").val(),
Keyword7 : $("#keyword7").val(),
Keyword8 : $("#keyword8").val(),
}),
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (result){
alert('done');
}
)};
很公平。但是,如何将参数从HTML页面获取到类中呢@使用(Html.BeginForm(“加载”,“高级搜索”,new@Model=关键字)我在回答中说MVC引擎将为您进行映射,哇,太好了。谢谢,我现在就尝试,并在尝试后立即接受答案!谢谢!非常感谢,它一下子就成功了。我真的低估了MVC引擎的效率。这太简单了!我已经有了类,但在方法中创建了它使用它作为参数!很聪明。但是有任何像字典这样的键来提供不同关键字的线索不是很聪明吗?如果你需要的话。你也可以扩展列表,使其具有一个带有标识符的自定义类型。我认为在ASP.NET MVC中,将其作为字典将是一个问题。好吧!我喜欢这种方式,干净、漂亮,并且避免所有属性!谢谢!以前没有使用过C#和MVC,所以还有很多技巧和窍门需要学习!:)谢谢你的回答,但是DavidG的解决方案效果非常好,所以我想我会用它来代替:)
function search() {
$.ajax({
type: "POST",
url: '@Url.Action("CreateEmail", "Email")',
data: JSON.stringify({
Keyword1 : $("#keyword1").val(),
Keyword2 : $("#keyword2").val(),
Keyword3 : $("#keyword3").val(),
Keyword4 : $("#keyword4").val(),
Keyword5 : $("#keyword5").val(),
Keyword6 : $("#keyword6").val(),
Keyword7 : $("#keyword7").val(),
Keyword8 : $("#keyword8").val(),
}),
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (result){
alert('done');
}
)};