gpt4 book ai didi

c# - 在 ActionLink 的 RouteValues 中传递 IEnumerable 属性

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:02 26 4
gpt4 key购买 nike

想象一个定义如下的对象:

public class MyViewModel{
public List<string> MyList { get; set; }
}

在我看来,我有这个操作链接:

@Ajax.ActionLink("<", "Index", new MyViewModel() { MyList = new List<string>() {"foo", "bar"}}, new AjaxOptions())

ActionLink 的 html 结果将是:

<a class="btn btn-default" data-ajax="true" href="/Index?MyList=System.Collections.Generic.List%601%5BSystem.String%5D">&lt;</a>

我的问题是,如何得到这个结果:

<a class="btn btn-default" data-ajax="true" href="/Index?MyList=foo&MyList=bar">&lt;</a>

最佳答案

你可以试试string.Join。像这样的东西

@Ajax.ActionLink(
"Your text", -- <
"ActionName", -- Index
new
{
MyList =string.Join(",", new List<string>() {"foo", "bar"}),
otherPropertiesIfyouwant = YourValue
}, -- rounteValues
new AjaxOptions { UpdateTargetId = "..." }, -- Your Ajax option --optional
new { @id = "back" } -- Your html attribute - optional
)

关于c# - 在 ActionLink 的 RouteValues 中传递 IEnumerable 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46304796/

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