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[]   

     }
3
  • How do you assign tags variables in your first action? Commented Apr 22, 2014 at 11:49
  • It assigned by input elements on html. Commented Apr 22, 2014 at 12:00
  • 1
    Try using the RouteValueDictionary overload instead of passing just an anonymous object. Commented Apr 22, 2014 at 12:18

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

1 Comment

@balron, if it is solved your problem, you can mark this as answer ;)
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

Comments

1

you can change string[] for List< string>

2 Comments

now,it cannot pass list values to 2nd action method
can you use viewbag? or session?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.