将项目添加到Razor视图中的字符串数组 [英] Add Item To String Array In Razor View
问题描述
我在一个剃须刀视图中声明一个字符串数组
@ {var siteList = new List< string>();var stockTypeList = new List< string>();}
在multiselect上有一个事件会遍历所选值,我想将每个所选项目的值添加到此数组中
$('#stockTypeArea select:selected').each(function(i,selected){@ stockTypeList.Add(selected.val());});
当前,它抱怨无法解析选定"值.所以我的问题是如何将这些字符串值添加到我的字符串数组中?
我需要字符串数组的原因是,我将其作为@ Html.Action的一部分发回了</p>
@ HTML.Action("CustomReport_Data","Reports",新的{stockCodesList = stockTypeList}))))
也许有更好的方法来捕获此字符串列表并将其传递回控制器.
即使我尝试使用以下方法测试对字符串数组的添加方法:
@ stockTypeList.Add("Test");
我收到一条错误消息:
Expression必须返回一个值以呈现
剃刀代码正在服务器端运行.
jQuery运行在客户端,事件处理程序也在运行.
此:
$('#stockTypeArea select:selected').each(function(i,selected){@ stockTypeList.Add(selected.val());});
是两者的结合,但这是行不通的. selected.val()
是客户端, @ stockTypeList.Add(...)
是服务器端.
所有这些都解释了您的第一条错误消息.
@ stockTypeList.Add("Test");
也是Razor(服务器端)语句.它将字符串添加到列表中,但是该方法未返回值( public void Add(T item)
). @
Razor运算符采用以下表达式,并将其作为URL编码的字符串写入客户端文档,但您未向其提供合法值( void
)./p>
这说明了您的第二个错误.
@ HTML.Action(...)
生成一个带有URL的链接,该URL可以包含查询字符串参数.这些通常来自调用中的第三个参数:
@ HTML.Action("CustomReport_Data","Reports",新的{stockCodesList = stockTypeList}))))
但是在执行此代码时(服务器端),列表当然是空的.
您需要在客户端代码(例如JavaScript数组)中收集字符串,并使用这些字符串构建用于导航的URL.
I have a razor view where I am declaring a string array
@{
var siteList = new List<string>();
var stockTypeList = new List<string>();
}
There is an event on a multiselect that iterates over the selected values and I want to add the value of each of the selected items to this array
$('#stockTypeArea select :selected').each(function (i, selected) {
@stockTypeList.Add(selected.val());
});
Currently this complains that it cannot resolve the 'selected' value. So my question is how can I add these string values to my string array?
The reason I need the string array is that I post this back as part of a @Html.Action
@HTML.Action("CustomReport_Data", "Reports", new { stockCodesList = stockTypeList })))
Perhaps there is a better way to capture this list of strings and pass back to the controller.
Even when I try and test the add method to my string array with:
@stockTypeList.Add("Test");
I get an error that:
Expression must return a value to render
Razor code is running server-side.
jQuery is running client-side, and so is your event handler.
This:
$('#stockTypeArea select :selected').each(function (i, selected) {
@stockTypeList.Add(selected.val());
});
Is a mix of both, but that can't ever work. selected.val()
is client-side, @stockTypeList.Add(...)
is server-side.
All of this explains your first error message.
@stockTypeList.Add("Test");
is also a Razor (server-side) statement. It adds a string to your list, but the method doesn't return a value (public void Add(T item)
). the @
Razor operator takes the following expression and writes it as a URL encoded string to the client document, but you're not providing it a legal value (void
).
This explains your second error.
@HTML.Action(...)
generates a link with a url that can include query string parameters. These are normally coming from the 3rd argument in your call:
@HTML.Action("CustomReport_Data", "Reports", new { stockCodesList = stockTypeList })))
But at the time you execute this code (server-side) the list is of course empty.
You need to collect your string in client-side code (JavaScript array for example) and construct a URL for navigation using these strings.
这篇关于将项目添加到Razor视图中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!