控制器返回数组的内容,但在我看来我只得到类型?多维控制
我有一个返回 viewModel 的控制器。
该 viewModel 有一个名为 Users 的属性,它是当前在系统中拥有帐户的所有用户名的数组。这用于 jquery 标记插件。
Users 属性正确地将数组发送到视图
但是当我在视图中使用 @Model.Users
时,我只得到类型 ie; System.String[]
我猜想解决这个问题非常简单。
控制器
public virtual ActionResult Create(int id)
{
var viewModel = new AttendeeViewModel();
var data = viewModel.GetMembershipUserList() ;
viewModel.MeetingID = id;
viewModel.Users = data.ToArray();
return View(viewModel);
}
ViewModel
public class AttendeeViewModel
{
public int AttendeeId { get; set; }
public string Name { get; set; }
public int MeetingID { get; set; }
public string[] Users { get; set; }
public List<string> GetMembershipUserList()
{
List<string> userNames = new List<string>();
foreach (MembershipUser user in Membership.GetAllUsers())
{
userNames.Add(user.UserName);
}
return userNames;
}
}
视图用法
<script type="text/javascript">
var tags = @Model.Users
$(document).ready(function () { $("#mytags").tagit({ availableTags: tags }); });
</script>
I Have a controller that returns a viewModel.
That viewModel has a property called Users, which is an array of all usernames that currently have an account in the system. This is used for a jquery tagging plugin.
The Users property correctly sends an array to the view
But when i use @Model.Users
in the view i just get the type ie; System.String[]
I guessing this is something ridiculously simple to solve.
Controller
public virtual ActionResult Create(int id)
{
var viewModel = new AttendeeViewModel();
var data = viewModel.GetMembershipUserList() ;
viewModel.MeetingID = id;
viewModel.Users = data.ToArray();
return View(viewModel);
}
ViewModel
public class AttendeeViewModel
{
public int AttendeeId { get; set; }
public string Name { get; set; }
public int MeetingID { get; set; }
public string[] Users { get; set; }
public List<string> GetMembershipUserList()
{
List<string> userNames = new List<string>();
foreach (MembershipUser user in Membership.GetAllUsers())
{
userNames.Add(user.UserName);
}
return userNames;
}
}
View Usage
<script type="text/javascript">
var tags = @Model.Users
$(document).ready(function () { $("#mytags").tagit({ availableTags: tags }); });
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Model.Users
相当于将Model.Users.ToString()
转储到视图中。对于您的特定用途,您需要将其呈现为 JavaScript 数组。 C# 字符串数组不知道如何做到这一点。类似的东西:虽然从 ViewModel 执行此操作可能更好,并且只有
Model.UsersAsJavaScriptArray
或类似的东西。如果我经常这样做,我可能会为 HtmlHelper 创建一个扩展方法并执行@Html.AsJavaScriptArray(Model.Users)
@Model.Users
is the equivalent to dumpingModel.Users.ToString()
into the view. For your particular use you need to render it as a JavaScript array. A C# string array has no idea how to do this. Something like :Although it's probably better to do this from the ViewModel and just have
Model.UsersAsJavaScriptArray
or something like that. If I was doing this often I'd probably create an extension method for HtmlHelper and do@Html.AsJavaScriptArray(Model.Users)
就像马特说的,当你尝试使用 <%= %> 输出内容时或者@它只是去.ToString()它。
相反,你可以尝试......
或者甚至只序列化你需要的东西
......
Like Matt said when you try to output something using <%= %> or @ it's just going to .ToString() it.
Instead you could try...
Or even only serialize the things you need...
...