List passed in ViewBag gets spoiled with “

2019-07-18 23:57发布

问题:

I think I have a very noob question but there it is:

I have a link to another controller and I want to pass a list of strings.

Controller:

[HttpPost]
public ActionResult Index(DateTime? dateFilter, int? Provider, int? middleOffice)
{
...
ViewBag.ReasonGroupName = new List<string>() { "Faults" };
...
}

View:

@Html.ActionLink(item.username, "Edit", "Hours", new { IdUser = item.IdUser, ReasonGroupNames = (List<string>)ViewBag.ReasonGroupName }, new { @class = "iframeFull" })

In my controller the ViewBag.ReasonGroupName is created properly: a list with the index 0 item ([0]) with the string "Faults"but when I receive it in my other controller the 0 index element of my list comes with "System.Collections.Generic.List1[System.String]" instead of "Faults"

I also try changing ViewBag for ViewData object but same problem appear.

Any ideia on what I'm doing wrong?

Thanks

回答1:

The text "System.Collections.Generic.List1[System.String]" is what you get if you call ToString() on an instance of the List type. When you generate the link the url has to be a string, hence ToString() is called on each argument.

It is not clear what information you are trying to send via the action link. If you want the first value then you could change the link to be the following:

@Html.ActionLink(item.username, "Edit", "Hours", new { IdUser = item.IdUser, ReasonGroupNames = ((List<string>)ViewBag.ReasonGroupName)[0] }, new { @class = "iframeFull" })

This will set the ReasonGroupName to the first value in the list.

But you probably want to send all the values in the list and not just the first. In that case you could join all the values of the list into a string and pass that as the argument instead.

So in your controller you could do:

ViewBag.ReasonGroupName = string.Join(",", yourListVariableHere);

This will convert the list into a comma separated string that can be included in the url properly.



回答2:

The @Html.ActionLink() method is used generate a url link to the given Controller/Action. Thus, it can only contain parameters that can be passed in the url of the link.

So I don't think you can pass an object through on the url.



回答3:

You are trying to send through get-request some complex data. As first, that is bad idea anyway, because in one good moment you can overcome limit for link.

You have solutions:

  1. Stay on sending get-request: serialize list to string - use special serializers or use string.Join(",", list); your edit method must accept string, which you deserialize to List by special deserializer or by string.Split method. Using of string.Join and string.Split is bad idea if your strings can contain char ','

  2. Don't send data. You can store it in db/session and then send id of saved data. You must be accurately: you must delete old data from db/session, but not new data

  3. Use post-request. Make form with @Html.BeginForm(...); in form with for add hidden input (@Html.HiddenFor(...)), and add submit button <input type="submit">, style it as link.



回答4:

Your problem is due to the fact that you're trying to pass a List of strings but have assigned it to a string property in the ActionLink(). You'll need to create a small helper to explode this List out to a single formatted string of your choice. This will then be passed to the url as a (for example) comma separated string. You'd then do some magic in the controller action to rehydrate this as required back to the List.