循环遍历数组并组装一个字符串

时间:2011-09-12 19:46:04

标签: c# arrays asp.net-mvc-3

public ActionResult Create(RecurringTask recurringTask, FormCollection collection, ICollection<string> dayOfTheWeek)

我试图遍历dayOfTheWeek(这是一组复选框),我试图找出哪一个是真的,所以我可以用它来组装字符串ex:星期一,星期二等等。

我很难找到循环通过我的集合来实现它的方法。我一直得到不能应用== to type string to bool error。

var days = dayOfTheWeek.ToString();
                    foreach (string day in dayOfTheWeek)
                    {
                        if(day == true)
                        {

                        }
                    }
                    recurringTask.DaysOfTheWeek = days;

这就是我在思考如何做到这一点。但我想有人在那里有一个比我更好的想法。那天== true给了我那个字符串bool错误,它明显地说明了它发生的原因,我只是不知道如何绕过它。

我的观点是:

<input type="checkbox" name="dayOfTheWeek" value="Monday" />
<input type="checkbox" name="dayOfTheWeek" value="Tuesday" />
<input type="checkbox" name="dayOfTheWeek" value="Wednesday" />
<input type="checkbox" name="dayOfTheWeek" value="Thursday" />
<input type="checkbox" name="dayOfTheWeek" value="Friday" />
<input type="checkbox" name="dayOfTheWeek" value="Saturday" />
<input type="checkbox" name="dayOfTheWeek" value="Sunday" />

3 个答案:

答案 0 :(得分:2)

错误消息很有启发性。您正尝试将字符串daytruefalse进行比较。 string是真还是假是什么意思?

您真的想看看是否选中了复选框。你声称dayOfTheWeek是一组CheckBoxes,但你正在调用它ToString(),它会将它转换为字符串列表。 var days = dayOfTheWeek.ToString();之前的代码是什么?您在哪里声明dayOfTheWeek

一旦你确实拥有List<Checkbox>,你真的想要遍历该列表以查看是否检查了每个元素。

foreach(CheckBox cb in dayOfTheWeek)
{
    if(cb.Checked)
    {
      // Logic
    }
}

答案 1 :(得分:2)

如果您将复选框命名为“dayOfTheWeek”,则ICollection<string> dayOfTheWeek仅包含所选复选框。

因此您可以将此列表视为“结果”,但我建议您使用有效值列表过滤日期列表以防止恶意攻击。

答案 2 :(得分:2)

  

public ActionResult Create(RecurringTask recurringTask,FormCollection collection,ICollection dayOfTheWeek)

很抱歉,但这可能是我见过的最糟糕的行动签名之一。域模型,FormCollection和一些ICollection<string>的混合。

使用视图模型,强类型视图和编辑器模板(这可能是我在StackOverflow上写这句话以回应asp.net-mvc标记中的问题的第10次)!它们将使您的生活变得更加轻松。所以是一个天数列表和一个相应的布尔属性,用于指示是否选择了这一天:

public class MyViewModel
{
    public IEnumerable<DayOfWeekViewModel> DaysOfWeek { get; set; }

    ... put any other properties that you consider useful for this view
}

public class DayOfWeekViewModel
{
    public string DayOfWeek { get; set; }
    public bool IsSelected { get; set; }
}

然后是控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            DaysOfWeek = CultureInfo
                .CurrentCulture
                .DateTimeFormat
                .DayNames
                .Select(x => new DayOfWeekViewModel
                {
                    DayOfWeek = x,
                })
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // model.DaysOfWeek will contain all you need here
        // TODO: do some processing
        // here you can loop through model.DaysOfWeek to identify which 
        // days have been selected and take respective actions
        // ...

        // once you have finished processing you could redirect
        return RedirectToAction("success");
    }
}

然后是相应的观点:

@model MyViewModel

@using (Html.BeginForm())
{
    ... you could put any other fields from your view model that
        will be used by this form here

    @Html.EditorFor(x => x.DaysOfWeek)
    <input type="submit" value="OK" />
}

和相应的编辑器模板(~/Views/Home/EditorTemplates/DayOfWeekViewModel.cshtml):

@model DayOfWeekViewModel

<div>
    @Html.CheckBoxFor(x => x.IsSelected) @Html.DisplayFor(x => x.DayOfWeek)
    @Html.HiddenFor(x => x.DayOfWeek)
</div>