Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 22, 2012 07:09 AM by NoFake3D
Member
6 Points
7 Posts
Dec 21, 2012 04:57 PM|LINK
Hi
I have a knowledge problem with my first asp.net MVC 4 razor based project and DropDownList sorting:
Model Appartement:
[Key] [ScaffoldColumn(false)] public Guid GUID { get; set; } [Required] [StringLength(20, MinimumLength = 3)] [DisplayName("Name des Appartements")] public string Name { get; set; } ...
Model Appartement_Buchung:
... [Required] [ForeignKey("Appartement")] [DisplayName("Appartement")] public Guid Appartement_GUID { get; set; } public virtual Appartement Appartement { get; set; }
Views-Appartement_Buchungen-Create.cshtml:
@model Sonnenwald_Appartements_MVC4_Webanwendung.Models.Appartement_Buchung ... <div class="editor-label"> @Html.LabelFor(model => model.Appartement_GUID, "Appartement") </div> <div class="editor-field"> @Html.DropDownListFor(model => model.Appartement_GUID, null) @Html.ValidationMessageFor(model => model.Appartement_GUID)
Now i have a DropDownList with unsorted appartement names, but i want to enable sorting by using DropDownListFor and OrderBy:
Similar to this: @Html.DropDownListFor(model => model.Appartement_GUID, new SelectList("Appartement_GUID", "?", "?").OrderBy(l => l.?))
Any suggestions will be greatly useful for me.
Thanks
Daniel
130 Points
47 Posts
Dec 21, 2012 06:35 PM|LINK
Can you sort them in the controller?
Dec 21, 2012 09:57 PM|LINK
Hi ovation22,
Good point. I forgot the controller:
// // GET: /Appartement_Buchungen/Create public ActionResult Create() { ViewBag.Appartement_GUID = new SelectList(Datenbank_Kontext_aktuell.Appartements, "GUID", "Name"); ...
I suppose i have no chance to sort the appartement names in the controller? I'm using ViewBag and SelectList here.
Can you give me a hint?
Dec 21, 2012 10:46 PM|LINK
What about:
ViewBag.Appartement_GUID = new SelectList(Datenbank_Kontext_aktuell.Appartements.OrderBy(x => x.Name), "GUID", "Name");
Dec 22, 2012 07:09 AM|LINK
It works! Many thanks!
Affectinate regards,
NoFake3D
Member
6 Points
7 Posts
Problem with @Html.DropDownListFor and OrderBy
Dec 21, 2012 04:57 PM|LINK
Hi
I have a knowledge problem with my first asp.net MVC 4 razor based project and DropDownList sorting:
Model Appartement:
[Key] [ScaffoldColumn(false)] public Guid GUID { get; set; } [Required] [StringLength(20, MinimumLength = 3)] [DisplayName("Name des Appartements")] public string Name { get; set; } ...... [Required] [ForeignKey("Appartement")] [DisplayName("Appartement")] public Guid Appartement_GUID { get; set; } public virtual Appartement Appartement { get; set; }Views-Appartement_Buchungen-Create.cshtml:
@model Sonnenwald_Appartements_MVC4_Webanwendung.Models.Appartement_Buchung ... <div class="editor-label"> @Html.LabelFor(model => model.Appartement_GUID, "Appartement") </div> <div class="editor-field"> @Html.DropDownListFor(model => model.Appartement_GUID, null) @Html.ValidationMessageFor(model => model.Appartement_GUID)Now i have a DropDownList with unsorted appartement names, but i want to enable sorting by using DropDownListFor and OrderBy:
Similar to this: @Html.DropDownListFor(model => model.Appartement_GUID, new SelectList("Appartement_GUID", "?", "?").OrderBy(l => l.?))
Any suggestions will be greatly useful for me.
Thanks
Daniel
ovation22
Member
130 Points
47 Posts
Re: Problem with @Html.DropDownListFor and OrderBy
Dec 21, 2012 06:35 PM|LINK
Can you sort them in the controller?
NoFake3D
Member
6 Points
7 Posts
Re: Problem with @Html.DropDownListFor and OrderBy
Dec 21, 2012 09:57 PM|LINK
Hi ovation22,
Good point. I forgot the controller:
// // GET: /Appartement_Buchungen/Create public ActionResult Create() { ViewBag.Appartement_GUID = new SelectList(Datenbank_Kontext_aktuell.Appartements, "GUID", "Name"); ...I suppose i have no chance to sort the appartement names in the controller? I'm using ViewBag and SelectList here.
Can you give me a hint?
Thanks
Daniel
ovation22
Member
130 Points
47 Posts
Re: Problem with @Html.DropDownListFor and OrderBy
Dec 21, 2012 10:46 PM|LINK
What about:
NoFake3D
Member
6 Points
7 Posts
Re: Problem with @Html.DropDownListFor and OrderBy
Dec 22, 2012 07:09 AM|LINK
Hi ovation22,
It works! Many thanks!
Affectinate regards,
Daniel