I have this:
[code]
public static IEnumerable
{
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 SelectListItem
{
Text = new
{
maandag = firstWeek.AddDays(j * 7).ToShortDateString()
//Dindsdag = x1.weekStart.AddDays(1).ToShortDateString(),
//vrijdag = x1.weekStart.AddDays(6).ToShortDateString(),
//weekNum = j + 1
}.ToString(),
//Value = (j + 1).ToString(CultureInfo.InvariantCulture)
};
}
//return helper.DropDownList()
}//end method
[/code]
And this:
[code]
public static MvcHtmlString StateDropDown(this HtmlHelper helper,string name, DateTime date)
{
return MvcHtmlString.Create(value: Enumerable.Select(GetWeekNumbers(helper), x => x.Text).ToString());
//return MvcHtmlString.Create((GetWeekNumbers(helper).First().Text));
}//end method
[/code]
and for the view:
[code]
td>
@Html.LabelFor(model => model.Tuesday, "Dinsdag")
@Html.TextBox("Hallo", Html.StateDropDown(null, new DateTime()))
@Html.ValidationMessageFor(model => model.Tuesday)
[/code]
But i get as output:
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Web.Mvc.SelectListItem,System.String]
in the texbox
THX for helping.
albert albertPosted May 22, 2012, 5:33 PM
but I have this: { maandag = 9-1-2012, Dindsdag = 10-1-2012 }
How I return now only the first item of the first value, I mean only this item: maandag = 9-1-2012, without Dindsdag?
THX.
albert albertPosted May 22, 2012, 4:55 PM
the javescript is working!!
[code]
function Selectedchange() {
$("#txtBox").text($("#myddl").text());
}
[/code]
But if it is value:
function Selectedchange() {
$("#txtBox").value($("#myddl").value());
}
then I get the weeknumber. But I want to have the text(Date). SO I try it like this:
function Selectedchange() {
$("#txtBox").text($("#myddl").text());
}
but doesnt work. How to get the text?
THX
albert albertPosted May 22, 2012, 9:31 AM
http://odetocode.com/Blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx
But I dont know what to do with this parameter in the view: int selectedId.
Niels.
albert albertPosted May 22, 2012, 9:03 AM
because it is a list of weeknumbers.
But this is the newest update.
But u dont get errors.
only in the view u get an error.Because I dont know what kind of parameters I have to set there:
[code]
@Html.LabelFor(model => model.Tuesday, "Dinsdag")
@Html.TextBox("WeekID", Html.StateDropDown())
@Html.ValidationMessageFor(model => model.Tuesday)
[/code]
Jignesh TrivediPosted May 22, 2012, 8:51 AM
I have tried but it through lot of error..
anyway....
In your above code return type StateDropDown is change to IEnumerable
albert albertPosted May 22, 2012, 8:32 AM
But did u ran the project.
Because the values has to be saved in the databse.
[code]
public static IEnumerable
{
// return MvcHtmlString.Create(value: GetWeekNumbers(helper).Where(g => g.Selected == true).Select(x => x.Text).FirstOrDefault());
//return MvcHtmlString.Create(value: GetWeekNumbers(helper).Select(x => x.Text != null ? x.Text : null).ToString());
//return MvcHtmlString.Create(value: Enumerable.Select(GetWeekNumbers(helper), x => x.Text).FirstOrDefault().ToString());
// return MvcHtmlString.Create(value: GetWeekNumbers(helper).Select(x => new SelectListItem
// {
// Selected = true ,
// Text = x.Text ,
// Value = x.Value
// }).ToString());
return weeks.OrderBy(week => week.WeekNumber).Select(week => new SelectListItem
{
Selected = (week.WeekID == selectedID),
Text = week.Name,
Value = week.WeekID.ToString(CultureInfo.CurrentUICulture )
});
//var listItems = GetWeekNumbers(helper);
// var selectedItem = listItems.First(item => item.Value == weeks );
//if (selectedItem != null) selectedItem.Selected = true;
//return MvcHtmlString.Create(value: listItems.Value);
//Text = x.Text(x),
// Value = x.Value(x)
//}).ToList());
// return MvcHtmlString.Create((GetWeekNumbers(helper).First().Text));
//return MvcHtmlString.Create((GetWeekNumbers(helper).First().Text));
}//end method
[/code]
if u run the project u can see it how it works. But I dont know what I have to put in the view:
[code]
@Html.LabelFor(model => model.Tuesday, "Dinsdag")
@Html.TextBox("WeekID", Model.Week )
@Html.ValidationMessageFor(model => model.Tuesday)
[/code]
Jignesh TrivediPosted May 22, 2012, 8:27 AM
You can achive this using Jquery...
First include Jquery min file.
<%:Html.DropDownList("myddl", Html.GetWeekNumbers(), new { onchange = "Selectedchange();" })%>
<%:Html.TextBox("txtBox")%>
Sorry I don't know Razor syntex..
Try it..
albert albertPosted May 22, 2012, 6:40 AM
I try it like this:
return MvcHtmlString.Create(value: GetWeekNumbers(helper).ElementAt(3).Text);
and so it gives me the correct output.
But selecting a value still doens work. I realy dont understand why the "select" function doesn't work.
Jignesh TrivediPosted May 22, 2012, 6:22 AM
this means that your collection of selectListItem has no selected value.
use
public static MvcHtmlString StateDropDown(this HtmlHelper helper,string name, DateTime date)
{
return MvcHtmlString.Create(value: GetWeekNumbers(helper).Where(g=>g.Selected == true).Select(x => x.Text).FirstOrDefault());
}
if you value is not selected at load time then textbox is blank
albert albertPosted May 22, 2012, 6:15 AM
Object reference not set to an instance of an object.
Jignesh TrivediPosted May 22, 2012, 6:01 AM
I think For the first time this will fine, when you change the value from dropdown you can handle is via javascript.
now que is text box contain only single property and selectlistItem has three property Text, Value and selected.
public static MvcHtmlString StateDropDown(this HtmlHelper helper,string name, DateTime date)
{
return MvcHtmlString.Create(value: GetWeekNumbers(helper).Where(g=>g.Selected == true).Select(x => x.Text).FirstOrDefault().ToString());
}
hope this will help you.
albert albertPosted May 22, 2012, 5:35 AM
That is how it has to be.
Jignesh TrivediPosted May 22, 2012, 5:32 AM
mvcHTMLstring is alway return html string.
I am not getting ur point.
you mean how we can select value which is selected from dropdown.
can you please provide info like what you required?
thx.
albert albertPosted May 22, 2012, 5:16 AM
How can u return the selected value from the dropdownlist then?
Jignesh TrivediPosted May 22, 2012, 5:12 AM
Reason is already define in above chain.
you fatch collection of SelectListItem and you can call ToString() extension method.this means collection.Tostring(). it always return name of collection property.
return MvcHtmlString.Create(value: GetWeekNumbers(helper).Select(x => new SelectListItem()
{
Selected = true,
Text = x.Text ,
Value = x.Value,
}).ToString());
this will always return wrong value, because it a collection
hope you understand.
albert albertPosted May 22, 2012, 4:46 AM
[code]
public static MvcHtmlString StateDropDown(this HtmlHelper helper,string name, DateTime date)
{
//return MvcHtmlString.Create(value: GetWeekNumbers(helper).Select(x => x.Text != null ? x.Text : null).ToString());
//return MvcHtmlString.Create(value: Enumerable.Select(GetWeekNumbers(helper), x => x.Text).FirstOrDefault().ToString());
return MvcHtmlString.Create(value: GetWeekNumbers(helper).Select(x => new SelectListItem()
{
Selected = true,
Text = x.Text ,
Value = x.Value,
}).ToString());
//Text = x.Text(x),
// Value = x.Value(x)
//}).ToList());
//return MvcHtmlString.Create((GetWeekNumbers(helper).First().Text));
//return MvcHtmlString.Create((GetWeekNumbers(helper).First().Text));
}//end method
[/code]
But I still get strange output back:
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Web.Mvc.SelectListItem,System.Web.Mvc.SelectListItem]
albert albertPosted May 22, 2012, 4:06 AM
but I already had that: return MvcHtmlString.Create((GetWeekNumbers(helper).First().Text));
This gives the same result.
But it has to return the selected date of the dropdownlist. So not only the first or last date.
If u can help me more, thx.
I have attached the hole project.
Niels.
Jignesh TrivediPosted May 22, 2012, 12:45 AM
GetWeekNumbers method is return collection of SelectListItem so behaviour of extension method is correct. and when select x => x.Text it return collection of string.
Try...
public static MvcHtmlString StateDropDown(this HtmlHelper helper, string name, DateTime date)
{
return MvcHtmlString.Create(value: Enumerable.Select(GetWeekNumbers(helper), x => x.Text).FirstOrDefault().ToString());
}
It only return first value...
Hope this will help you.