gpt4 book ai didi

c# - ASP.NET MVC - 无法将 string[] 值传递给另一个 Controller 操作

转载 作者:行者123 更新时间:2023-12-02 04:30:42 25 4
gpt4 key购买 nike

我正在尝试将字符串数组(有两个值)从一个 Controller 操作传递到另一个操作。但第二个 Action 只有这个值:'System.String[]'

它将值从 View 传递到 Controller 操作。但是当我尝试传递另一个值时,它只传递空字符串。

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

}

最佳答案

我认为最好使用 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"];
}

祝你好运!

关于c# - ASP.NET MVC - 无法将 string[] 值传递给另一个 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com