-1

So in my mvc 5 app, I got this default 'Index' action, which simply redirects to the search action, with the default model values:

    [Route]
    public ActionResult Index()
    {
        var model = new T();

        return RedirectToAction("Search", model);
    }

What i got puzzled about is how i end up with the url like '.../search?xxx=xxx...'? Is there anything i can do to customize or at least inject/replace the url generation, especially the query string part? e.g. i might want to display 1/0 for bool properties in the search model, and customize query string key names etc?

and why would someone vote down for my question? psst...

Nico
  • 497
  • 1
  • 4
  • 15
  • 1
    This has been answered here: http://stackoverflow.com/questions/1067200/how-to-add-querystring-values-with-redirecttoaction-method – Dessus May 02 '17 at 04:20
  • @Dessus, what i'm curious about is the seemingly 'serialization' process, which generates url from the passed-in route value dictionary, or any model types. I know what it generates based on my input, but i don't know how and how to customize specifically. – Nico May 02 '17 at 07:37
  • You can download the nuget symbols for asp.net and have a look how RedirectToAction works. – Dessus May 02 '17 at 20:27

1 Answers1

0

The query string generated depend on the model property name and type + value you passed in.

For example, say if your pass in model is like

public class MyModel
{
   bool IsSort{get;set;}
}

If your model is like this and IsSort value is true, then you will get url like /search?IsSort=true

Say you want to change your query string to 1/0 instead of true or false, then create viewModel which has property string then assing it properly like:

public class MyModel
{
   string IsSort{get;set;}
}

var model = new MyModel();
model.IsSort = true? "1":"0";

same apply to querystring key (which correspond to property name)

Alan Tsai
  • 2,465
  • 1
  • 13
  • 16
  • This works, but not elegant IMHO. Is there anything like what [JsonProperty] does, so that i can do more customization? – Nico May 02 '17 at 07:38
  • Or at least, there is a global place to make this customization setting, instead of writing everywhere. – Nico May 02 '17 at 09:44
  • @Nico True it is not elegant for whole site. I haven't try it, but to achieve globally, I think you need to do two thing 1. add custom model binder - so when 1 is provided, it can bound to bool true. 2. change how url generate - so when generating url, generate 1 instead of true for boolean. model binding have a look at: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding, change url you can write your own RedirectToAction method - asp .net mvc is opens ource, so you can use some of its logic and add your custom bit to achieve your goal. I would say it is quite some work. – Alan Tsai May 02 '17 at 10:16
  • thanks. marked your reply as the answer, although it doesn't really solve my problem. mvc should really provide the 2-way model binding customization. – Nico May 03 '17 at 01:51