-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Complex model binding with RedirectToAction #5093
Comments
I think the problem here is a feature that was added to better support lists of data in URL generation. Because I think you can explicitly convert the list of data to a string and then get the behavior you want: public IActionResult DoAction(FirstViewModel model)
{
return RedirectToAction("Index", "Second", model.ToString()); // Call ToString explicitly
} |
Doesn't work for me. Call ToString explicitly like this call the ToString from "FirstViewModel", no "SecondViewModel". |
I guess @Eilon meant Still in your example, you are passing try change this:
to this:
|
It works if "Second" is the lonely property of "FirstViewModel". And sometimes this is not the case. The goal, have a way for create custom query value, from some complex objects, like RC1 worked. |
@LeroyD ah, indeed, you are correct. I should have tried it before posting 😄 The reason this is happening is what I described before: starting in ASP.NET Core 1.0 RC2, the behavior of lists (IEnumerable) was updated to be comma-separated values. Because all dictionaries are lists, their behavior is changed. To revert to the previous behavior, you can wrap the model in a model that has the behavior that you want for routing, e.g. add these wrapper classes: public class FirstViewModelRouteData
{
public FirstViewModelRouteData(FirstViewModel first)
{
Second = new SecondViewModelRouteData(first.Second);
}
public SecondViewModelRouteData Second { get; set; }
}
public class SecondViewModelRouteData
{
public SecondViewModelRouteData(SecondViewModel second)
{
SecondModel = second;
}
private SecondViewModel SecondModel { get; }
public override string ToString()
{
return SecondModel.ToString();
}
} And then call routing by wrapping the original model in the route model like this: return RedirectToAction("Index", "Second", new FirstViewModelRouteData(model)); And then I get the expected URL |
Great, it works with this solution. Thanks. :) |
@LeroyD great! |
I recently update the framework version from RC1 to last version of Asp.Net core, and I have an issue with "RedirectToAction" method, with complex object to transfer.
RedirectToAction redirect my ViewModel to the action with GET parameters. This example :
Into the controller :
With Asp Net RC1, this working perfectly. The redirect is done with the good parameter :
"myUrl?Second=1|one#2=two"
And I can get this value with a custom model binding.
But with last version of Asp .Net core, the redirect passing a wrong value parameter. I can see this :
"myUrl?Second=[1,one]&Second=[2,two]"
So the model binding don't work.
If possible, I want to keep my custom parameter defined by the override ToString() like RC1 version.
The text was updated successfully, but these errors were encountered: