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>();
}
}
このようにビューを呼び出すことにより、「月曜日、...、土曜日」のチェックボックスリストが表示されます。
@Html.FullFieldEditor(m => m.QuotePartRecord.ContactClientDayCheckBox)
注:は、を繰り返し使用FullFieldEditor
する特別なヘルパーであり、ラジオボタンリスト、ドロップダウンリスト、チェックボックスリスト、または複数選択リストのいずれかを選択します。この場合、「6」を使用すると、チェックボックスリストが作成されます。 6つのアイテムのコレクション(つまり、日数)。enum
BulkSelectionThreshold
enum
私のコントローラーは、モデルの状態の有効性のみをチェックし、確認ビューに渡します。
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());
}
}