简体   繁体   中英

How to covert the complex type in viewdata to Query string

i am trying to implement the advanced searching with pagination functionality in a ASP.NET MVC application. i am trying to pass the search filter parameters in the QueryString. But as the search filter type is bit complex it is not converted correctly

Help me implement this/

Here is my action

public ActionResult AdSearch(SearchFilter SearchTerm, int index = 0, int perPage = 1)
{
    var customers = _repository.Search(SearchTerm, new SelectSpec(perPage, index * perPage));
    ViewData["SearchTerm"] = SearchTerm;
    return View("ExpertSearchResult", customers);
}

Here is the search filter

public class SearchFilter
{
    public IList<string> Countries { get; set; }
    public IList<string> Languages { get; set; }
    public IList<string> Industries { get; set; }
    public IList<string> Expertises { get; set; }        
}

And here is the code for generating page links

var searchCriteria = new RouteValueDictionary(ViewData["SearchTerm"]);searchCriteria.Add("perPage", 1);searchCriteria.Add("index", 1);return Html.ActionLink("First", "Results", searchCriteria)

the best solution for you is Json.NET . using json you can easily serialize and deserialize your complex types between javascript and your c# code.

just create your object in javascript (SearchFilter) and c#. then place json attributes on class and its Properties. then serialize you object in javascript using json and pass it to your controller action. in action you should have a string parameter to get serialized value and then you should ask json to deserialize parameter to your required object.

this is one of common usages of Json.NET

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