我一直在使用DelimitedStringHelper扩展来将一系列项目转换为带有IEnumerable<T>
的分隔字符串。默认情况下,对序列中的每个项目调用ToString()以使用默认分隔符“,”来表示结果。
这具有以下效果:
public enum Days
{
[Display(Name = "Monday")]
Monday,
//Left out other days for brevity
[Display(Name = "Saturday")]
Saturday
}
并与模型的其余部分结合使用:
[Mandatory(ErrorMessage = "Please select at least one day")]
[Display(Name = "What are the best days to contact you (select one or more days)?")]
[ContactClientDaySelector(BulkSelectionThreshold = 6)]
public List<string> ContactClientDayCheckBox { get; set; }
以及`[ContactClientDaySelector]:
的代码public class ContactClientDaySelectorAttribute : SelectorAttribute
{
public override IEnumerable<SelectListItem> GetItems()
{
return Selector.GetItemsFromEnum<ContactClientDay>();
}
}
将显示一个带有“Monday,...,Saturday”的复选框列表,方法是在视图中调用:
@Html.FullFieldEditor(m => m.QuotePartRecord.ContactClientDayCheckBox)
注意:FullFieldEditor
是一个特殊的助手,它遍历enum
并使用BulkSelectionThreshold
选择一个单选按钮列表,下拉列表,复选框列表或多选list - 在这种情况下,“6”将触发创建复选框列表,因为我的enum
有6个项目(即天)的集合。
我的控制器只检查模型状态的有效性并传递给确认视图:
public ActionResult WrapUp(string backButton, string nextButton)
{
if (backButton != null)
return RedirectToAction("ExpenseInformation");
else if ((nextButton != null) && ModelState.IsValid)
return RedirectToAction("Confirm");
else
return View(quoteData);
}
public ActionResult Confirm(string backButton, string nextButton)
{
if (backButton != null)
return RedirectToAction("WrapUp");
else if ((nextButton != null) && ModelState.IsValid)
{
var quoteConfirm = _quoteService.CreateQuote(quoteData.QuotePartRecord);
return RedirectToAction("Submitted");
}
else
return View(quoteData);
}
现在,为了发布用户选择的复选框,我在确认视图页面中使用了以下内容:
[ContactClientDaySelector]
[ReadOnly(true)]
public List<string> ContactClientDayCheckBoxPost
{
get { return ContactClientDayCheckBox; }
}
这与DelimitedStringHelper结合使用会显示选择。例如,如果用户选择了星期一和星期二,则帖子将显示“星期一,星期二”。
但是,我不得不改变我的代码,并且使用int
而不是专门用于复选框(长话短说:使用NHibernate会因为使用List<string>
而生成强制转换错误,并且这是解决这个问题的方法。)
我必须删除enum
并将其替换为此类:
public class ContactClientDay
{
public int Id { get; set; }
public string Name { get; set; }
}
然后我修改了我的Selector类:
public class ContactClientDaySelectorAttribute : SelectorAttribute
{
public ContactClientDaySelectorAttribute()
{
//For checkboxes, number must equal amount of items
BulkSelectionThreshold = 6;
}
public override IEnumerable<SelectListItem> GetItems()
{
var contactClientDay = new List<ContactClientDay>
{
new ContactClientDay {Id = 1, Name = "Monday"},
//Left out other days for brevity
new ContactClientDay {Id = 6, Name = "Saturday"},
};
return contactClientDay.ToSelectList(m => m.Id, m => m.Name);
}
}
我将模型改为:
public virtual int? ContactClientDayCheckBox { get; set; }
我将帖子更改为:
[ReadOnly(true)]
public string ContactClientDayCheckBoxPost
{
get { return QuotePartRecord.ContactClientDayCheckBox.ToString(); }
}
如果我改为public int? ContactClientDayCheckBoxPost
,确认页面上不会显示任何内容。
如果我改为使用public string ContactClientDayCheckBoxPost
然后执行ContactClientDayCheckBox.ToString()
,则只显示所选第一个值的“名称”(仅“星期一”,而不是“星期一,星期二”)。
在这种情况下,我无法弄清楚如何以编程方式转换int
的序列(可能带有扩展名)?有什么想法/例子吗?提前谢谢。
供参考,以下是我正在使用的DelimitedStringHelper
的扩展名:
public static class DelimitedStringHelper
{
public static string DefaultDelimiter = ", ";
/// <summary>
/// Convert a sequence of items to a delimited string. By default, ToString() will be called on each item in the sequence to formulate the result. The default delimiter of ', ' will be used
/// </summary>
public static string ToDelimitedString<T>(this IEnumerable<T> source)
{
return source.ToDelimitedString(x => x.ToString(), DefaultDelimiter);
}
/// <summary>
/// Convert a sequence of items to a delimited string. By default, ToString() will be called on each item in the sequence to formulate the result
/// </summary>
/// <param name="delimiter">The delimiter to separate each item with</param>
public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter)
{
return source.ToDelimitedString(x => x.ToString(), delimiter);
}
/// <summary>
/// Convert a sequence of items to a delimited string. The default delimiter of ', ' will be used
/// </summary>
/// <param name="selector">A lambda expression to select a string property of <typeparamref name="T"/></param>
public static string ToDelimitedString<T>(this IEnumerable<T> source, Func<T, string> selector)
{
return source.ToDelimitedString(selector, DefaultDelimiter);
}
/// <summary>
/// Convert a sequence of items to a delimited string.
/// </summary>
/// <param name="selector">A lambda expression to select a string property of <typeparamref name="T"/></param>
/// <param name="delimiter">The delimiter to separate each item with</param>
public static string ToDelimitedString<T>(this IEnumerable<T> source, Func<T, string> selector, string delimiter)
{
if (source == null)
return string.Empty;
if (selector == null)
throw new ArgumentNullException("selector", "Must provide a valid property selector");
if (string.IsNullOrEmpty(delimiter))
delimiter = DefaultDelimiter;
return string.Join(delimiter, source.Select(selector).ToArray());
}
}