从定界字符串填充列表属性 [英] Populate a list property from delimited string

查看:70
本文介绍了从定界字符串填充列表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的应用程序,但是我对ASP.NET MVC有点陌生,所以我在解决如何实现此目标方面遇到了一些麻烦,因为

This is kind of a simple application but I'm a bit new to ASP.NET MVC so I'm having a bit of trouble wrapping my head around how to accomplish this because the

我有两个班级:

public class BugAssignment
{
    public int BugAssignmentID { get; set; }
    public int BugNumber { get; set; }
    public int UserID { get; set; }
    public virtual User User { get; set; }
}

public class BugAssignmentList
{
    public int BugAssignmentListID { get; set; }
    public string Name { get; set; }
    public List<BugAssignment> BugAssignments { get; set; }
}

我想要做的是填充BugAssignments列表,给定用户输入的BugNumbers分隔字符串(即208,576,403).我猜想它会爆炸字符串,遍历并创建BugAssignment对象,存储到列表中并设置BugAssignments属性.我该怎么办?

What I want do is populate the List of BugAssignments given user inputted delimited string of BugNumbers from (i.e. 208,576,403). I'm guessing it would explode the string, loop through and create the BugAssignment objects, store into a list and set the BugAssignments property. How could I accomplish this?

此外,我将SQL Server 2008作为数据库引擎使用Entity Framework Code-first.

Also, I am using Entity Framework Code-first with SQL server 2008 as my database engine.

推荐答案

如果您坚持将字符串转换为字符串数组...

If you're stuck on converting the string to an array of strings...

使用 String.Split().

string[] StringArray = String.Split(BugAssignments , ",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

从那里开始,直接遍历数组并完成其余工作应该很简单.如果您遇到其他问题,请告诉我们.

From there, it should be straightforward to loop through the array and do the remainder of the work. If you're stuck on a different part, let us know.

这篇关于从定界字符串填充列表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆