控制器返回数组的内容,但在我看来我只得到类型?多维控制

发布于 2024-11-03 02:21:41 字数 1287 浏览 0 评论 0原文

我有一个返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞄了个咪的 2024-11-10 02:21:41

@Model.Users 相当于将 Model.Users.ToString() 转储到视图中。对于您的特定用途,您需要将其呈现为 JavaScript 数组。 C# 字符串数组不知道如何做到这一点。类似的东西:

 var tags = [ @( string.Join("," Model.Users.Select(s => "'" + s + "'").ToArray()) ) ];

虽然从 ViewModel 执行此操作可能更好,并且只有 Model.UsersAsJavaScriptArray 或类似的东西。如果我经常这样做,我可能会为 HtmlHelper 创建一个扩展方法并执行 @Html.AsJavaScriptArray(Model.Users)

@Model.Users is the equivalent to dumping Model.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 :

 var tags = [ @( string.Join("," Model.Users.Select(s => "'" + s + "'").ToArray()) ) ];

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)

枫以 2024-11-10 02:21:41

就像马特说的,当你尝试使用 <%= %> 输出内容时或者@它只是去.ToString()它。

相反,你可以尝试......

ViewModel.UsersJson = new JavascriptSerializer().Serialize(data.ToArray());

或者甚至只序列化你需要的东西

ViewModel.UsersJson = new JavascriptSerializer().Serialize(data.ToArray().Select(u => new { Username = u.Username, FirstName = u.FirstName });

......

var tags = @Model.UsersJson

Like Matt said when you try to output something using <%= %> or @ it's just going to .ToString() it.

Instead you could try...

ViewModel.UsersJson = new JavascriptSerializer().Serialize(data.ToArray());

Or even only serialize the things you need...

ViewModel.UsersJson = new JavascriptSerializer().Serialize(data.ToArray().Select(u => new { Username = u.Username, FirstName = u.FirstName });

...

var tags = @Model.UsersJson
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文