2

i am trying pass string array(that has two values) from a controller action to another another action. But second Action just have this value : 'System.String[]'

It passes values from view to controller action.But when i try to pass another it just pass empty string.

Html(Razor)

   @using (Html.BeginForm("AnnouncementCreate", "Administrator",   FormMethod.Post, new { id = "form" }))  
        {
         //There are another html elements....
      <label class="checkbox-inline">

            <input type="checkbox" name="tags" id="web" value="web" checked="checked" />
            <span>Web</span>
         </label>

        <label class="checkbox-inline ">
           <input type="checkbox" name="tags" id="ambulance" value="client" checked="checked" />
            <span>Ambulance Client</span>
        </label>

Controller:

public ActionResult AnnouncementCreate(NewsItem NEWS, Guid RSSCATEGORY,Guid NEWSIMAGETYPE,string[] tags)
 //tags variable have two values: [0]:web  , [1]:client    
 {
   ....

   //I want to Pass current string[] values to another Action  
  return RedirectToAction(actionName: "Announcements", routeValues: new { tags = tags });
    }

  //2nd action method 
  public ActionResult Announcements(string[] tags)
    {
      //tags variable has this value:  [0]: System.String[]   

     }
balron
  • 709
  • 4
  • 18
  • 40

3 Answers3

3

I think it is better to use TempData, like this:

public ActionResult AnnouncementCreate(NewsItem NEWS, Guid RSSCATEGORY,Guid NEWSIMAGETYPE,string[] tags)
 //tags variable have two values: [0]:web  , [1]:client    
{
   ....
    TempData["tags"] = tags;
    return RedirectToAction(actionName: "Announcements");
}

  //2nd action method 
public ActionResult Announcements()
{
    var tags = (string[]) TempData["tags"];
}

Good luck!

Boris Parfenenkov
  • 3,219
  • 1
  • 25
  • 30
1

MVC doesn't seem to like playing nicely with arrays.

If you insist on using them, this is the most elegant way I've found of achieving this.

working proof of concept...forgive the VB

  Dim rv As RouteValueDictionary = New RouteValueDictionary

  rv.Add("tags[0]", "Test1")
  rv.Add("tags[1]", "Test2")

  Return RedirectToAction("Announcements", "Home", rv)

Borrowed from here: ASP.Net MVC RouteData and arrays

Community
  • 1
  • 1
dtyrrell
  • 31
  • 2
1

you can change string[] for List< string>

AndrehOdon
  • 27
  • 7