【问题标题】:Passing a lot of parameters to a controller?向控制器传递大量参数?
【发布时间】:2015-01-10 14:38:51
【问题描述】:

我正在创建一个“高级输入表单”,其中包含大量用于搜索数据的输入。我的问题是:将大量数据从 HTML 传递到控制器的最佳方式是什么。

我问的原因是。假设你有这个 HTML 表单:

 @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;" /> 
    }

然后像这样将它全部传递给控制器​​会很讨厌(我有更多的关键字):

public ActionResult Loading(string keyword1, string keyword2, string keyword3, string keyword4, string keyword5, string6
                         string keyword7, string keyword8, string keyword9){
//do things to the parameters!
return View();
}

那么您将如何执行此操作,或者您会这样做吗?

谢谢!

【问题讨论】:

    标签: html asp.net asp.net-mvc-4


    【解决方案1】:

    使用模型类。如果输入名称与模型属性匹配,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();
    }
    

    【讨论】:

    • 很公平。但是如何从 HTML 页面获取参数到类呢? @using (Html.BeginForm("Loading", "AdvancedSearch", new @Model=Keywords)
    • 我在答案中已经说过MVC engine will do the mapping for you
    • 哇,太好了。谢谢,我现在就试一试,一试就接受答案!谢谢!
    • 非常感谢它立即起作用。我真的低估了MVC引擎。这太容易了!我已经有了这个类,但是在方法中创建了它而不是使用它作为参数!
    【解决方案2】:

    我建议使用视图模型并包含关键字列表。为每个关键字添加一个属性是没有意义的:

    public class Keywords
    {
        public List<string> Items { get; set; }
    }
    
    public ActionResult Loading(Keywords keywords){ }
    

    或者如果可能的话:

    public ActionResult Loading(List<string> keywords){ }
    

    阅读更多关于它的信息here

    【讨论】:

    • 聪明。但是,如果有任何键(如字典)来提供线索不同的关键字是什么,那不是很聪明吗?
    • 如果您需要。您还可以扩展 List 以拥有一个带有标识符的自定义类型。我认为使它成为字典在 ASP.NET MVC 中会是一个问题。
    • 好的!我喜欢这种方式,干净漂亮,避免所有属性!谢谢!之前没有使用过 C# 和 MVC,所以还有很多技巧和窍门需要学习! :)
    【解决方案3】:

    使用关键字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; }
    }
    

    然后是ActionResult比如

    public ActionResult Loading(SearchDto dto)
    {
    return View();
    }
    

    您可以从视图中发布您的数据。

    这里有一个例子Send JSON data via POST (ajax) and receive json response from Controller (MVC)

    还有这里

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

    【讨论】:

    • 感谢您的回答,但 DavidG 的解决方案效果很好,所以我想我会改用那个 :)
    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 2011-09-02
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    相关资源
    最近更新 更多