2014-04-22 82 views
2

我想從控制器動作傳遞字符串數組(有兩個值)到另一個另一個動作。 但是第二個動作只有這個值:'System.String []'ASP.NET MVC-無法傳遞字符串[]值到另一個控制器動作

它將值從視圖傳遞給控制器​​action.But當我嘗試傳遞另一個它只是傳遞空字符串。

HTML(剃刀)

@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> 

控制器:

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

    } 
+0

如何在第一個動作中分配'標籤'變量? –

+0

它由html上的輸入元素分配。 – balron

+1

嘗試使用RouteValueDictionary重載而不是僅傳遞匿名對象。 –

回答

3

我認爲這是更好使用TempData,如下所示:

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"]; 
} 

好l科索沃解放軍!

+0

謝謝,解決了這個問題。 – balron

+0

@balron,如果它解決了您的問題,您可以將其標記爲答案;) – idlerboris

+0

雖然我標記了它,現在它被標記:) – balron

1

MVC似乎並不喜歡使用數組很好打。

如果你堅持使用它們,這是我找到的最優雅的方式。

工作理念的證明......原諒VB

Dim rv As RouteValueDictionary = New RouteValueDictionary 

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

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

從這裏借:ASP.Net MVC RouteData and arrays

1

你可以改變的String []爲列表<字符串>

+0

現在,它無法將列表值傳遞給第二個操作方法 – balron

+0

您可以使用viewbag嗎?或會話? – AndrehOdon

相關問題