TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
albert albert
NA
524
0
optgroup in mvc 3
May 24 2012 8:15 AM
Hi Everybody,
I have this:
[code]
public static class GroupDropListExtensions
{
public static MvcHtmlString GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, int? SelectedValue, object htmlAttributes)
{
if (data == null && helper.ViewData != null)
data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>;
if (data == null) return new MvcHtmlString(string.Empty);
var select = new TagBuilder("select");
if (htmlAttributes != null)
select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
select.GenerateId(name);
select.MergeAttribute("name", name);
var optgroupHtml = new StringBuilder();
var groups = data.ToList();
foreach (var group in data)
{
var groupTag = new TagBuilder("optgroup");
groupTag.Attributes.Add("label", helper.Encode(group.Name));
var optHtml = new StringBuilder();
foreach (var item in group.Items)
{
var option = new TagBuilder("option");
option.Attributes.Add("value", helper.Encode(item.Value));
if (SelectedValue != 0 && item.Value == SelectedValue)
option.Attributes.Add("selected", "selected");
option.InnerHtml = helper.Encode(item.Text);
optHtml.AppendLine(option.ToString(TagRenderMode.Normal));
}
groupTag.InnerHtml = optHtml.ToString();
optgroupHtml.AppendLine(groupTag.ToString(TagRenderMode.Normal));
}
select.InnerHtml = optgroupHtml.ToString();
return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
}
/*
public static object GroupDropList(this HtmlHelper helper, string weekid, IEnumerable<GroupDropListItem> getWeekNumbers, object selectedValue)
{
}
*/
}
public class GroupDropListItem
{
public string Name { get; set; }
public List<OptionItem> Items { get; set; }
}
public class OptionItem
{
public string Text { get; set; }
public int Value { get; set; }
}
[/code]
and the extension method that will use the class:
[code]
public static IEnumerable<GroupDropListItem> GetWeekNumbers(this HtmlHelper helper /*Dictionary<int, DateTime> dayOfWeek*/ /*,DateTime allWeeks*/ /*int selectID*/)
{
var janFirst = new DateTime(DateTime.Today.Year, 1, 1);
//beware different cultures.
var firstWeek = janFirst.AddDays(1 - (int)(janFirst.DayOfWeek));
int j = -1;
foreach (var x1 in Enumerable.Range(0, 52).Select(i => new
{
weekStart = firstWeek.AddDays(i * 7),
Tuesday = firstWeek.AddDays(i * 3)
}).TakeWhile(x => x.weekStart.Year <= janFirst.Year).Select(x => new
{
x.weekStart,
tuesday = x.weekStart.AddDays(3),
weekFinish = x.weekStart.AddDays(6)
}).SkipWhile(x => x.weekFinish < janFirst.AddDays(1)))
{
j++;
yield return new GroupDropListItem(
{
List<GroupDropListItem> = new
{
Maandag = firstWeek.AddDays(j*7).ToShortDateString(),
//.OrderBy(item => item.ToString(CultureInfo.InvariantCulture)),
Dindsdag = x1.weekStart.AddDays(1).ToShortDateString(),
Wendsday = x1.weekStart.AddDays(2).ToShortDateString(),
Thursday = x1.weekStart.AddDays(3).ToShortDateString(),
Fryday = x1.weekStart.AddDays(4).ToShortDateString(),
Saterday = x1.weekStart.AddDays(5).ToShortDateString(),
Sunday = x1.weekStart.AddDays(6).ToShortDateString(),
//vrijdag = x1.weekStart.AddDays(6).ToShortDateString(),
//weekNum = j + 1
}.ToString(),
Name = (j + 1).ToString(CultureInfo.InvariantCulture);//.ToString(CultureInfo.InvariantCulture))
});
}
//return helper.DropDownList()
}//end method
[/code]
and for the view:
[code]
<div class="editor-field">
@Html.GroupDropList("weekId", (IEnumerable<GroupDropListItem>) Html.GetWeekNumbers(), new {onchange = "Selectedchange();"})
@Html.ValidationMessageFor(model => model != null && model.Week != null ? model.Week.WeekNumber : null)
</div>
[/code]
But how to get the helper class: GroupDropListExtensions
integrated in the othter helper method:
GetWeekNumbers.
THX for helping
Reply
Answers (
0
)
Multichekbox in DateTime Picker
urgent!