简体   繁体   中英

Passing a lot of parameters to a controller?

I am creating an "advanced input form" with a lot of inputs for searching data. My question is: What is the best way to pass a lot of data from HTML to the controller.

The reason i ask is. Lets say you have this HTML form:

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

Then it will be pretty nasty to pass it all to the controller like this (I've got a lot more keywords):

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

So how would you perform this action or would you do it like this?

Thanks!

Use a model class. Provided the input names match the model properties, the MVC engine will do the mapping for you.

public class Keywords
{
    public string keyword1 { get; set; }
    public string keyword2 { get; set; }
    ///etc...
}

And you action is much simpler:

public ActionResult Loading(Keywords keywords){
    //do things to the parameters!
    var keyword1 = keywords.keyword1;
    return View();
}

I would suggest to use a view model and include a list for your keywords. It is non-sense to add a property for every keyword:

public class Keywords
{
    public List<string> Items { get; set; }
}

public ActionResult Loading(Keywords keywords){ }

Or if possible:

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

Read some more about it here .

Create a class with those keyword1, keyword2 and so on...

Such as

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

Then ActionResult such as

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

You can post your data from view.

There is an example Send JSON data via POST (ajax) and receive json response from Controller (MVC) here.

And also here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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