asp.net-mvc-3 – 使用辅助方法对MVC3 / Razor中的DropDownList
嗨,所以我对MVC3和Razor都很陌生,过去几天我一直在努力解决这个问题.我的项目架构师给了我一个任务来创建一个帮助方法来对MVC视图中的下拉列表进行排序.我有一个View从Controller中检索各种数据,我正在返回一些我希望出现在下拉列表中的值.我被告知不要在Controller中对它进行排序,也要将我们想要排序的字段传递给helper方法.我可以像下面这样做,但建筑师希望保持视图没有尖锐的代码:
@Html.DropDownListFor(model => model.StudyName,new SelectList(ViewBag.StudyTypes,"Value","Text").OrderBy(l => l.Text)) 所以我创建了一些示例代码和一些扩展方法来尝试让它工作.我的想法是复制现有的Html.DropDownList方法并允许传递’object htmlAttributes’,这样我就可以将样式设置为方法调用的一部分. 到目前为止,这是我的代码.我在Edit Controller方法中返回ViewBag.StudyTypes中下拉列表的数据: public ActionResult Edit(int id) { IEnumerable<SelectListItem> mySelectList = new List<SelectListItem>(); IList<SelectListItem> myList = new List<SelectListItem>(); for (int i = 0; i < 5; i++) { myList.Add(new SelectListItem() { Value = i.ToString(),Text = "My Item " + i.ToString(),Selected = i == 2 } ); } mySelectList = myList; ViewBag.StudyTypes = mySelectList; StudyDefinition studydefinition = db.StudyDefinitions.Find(id); return View(studydefinition); } 这是我的查看代码: @model MyStudyWeb.Models.StudyDefinition @using MyStudyWeb.Helpers @{ ViewBag.Mode = "Edit"; } <div> @Html.DropDownListSorted(new SelectList(ViewBag.StudyTypes,"Text"))<br /> @Html.DropDownListSorted("MyList","Text"))<br /> </div> 下面是我试图开始工作的扩展方法.第一个扩展方法什么都不做,我只是在视图中的那个点得到一个空格.第二种方法有点工作,但它很难看.对于第三种方法,我不知道如何指定’order by’参数,因为IEnumerable上的OrderBy需要一个Linq表达式. namespace StudyDefinition.Helpers { public static class HtmlHelperExtensions { // 1st sort method: sort the passed in list and return a new sorted list public static SelectList DropDownListSorted(this HtmlHelper helper,IEnumerable<SelectListItem> selectList) { var x = new SelectList(selectList.ToList()).OrderBy(l => l.Text); return x as SelectList; } // 2nd sort method: return IHtml string and create <select> list manually public static IHtmlString DropDownListSorted(this HtmlHelper helper,string name,SelectList selectList) { StringBuilder output = new StringBuilder(); (selectList).OrderBy(l => l.Text); output.Append("<select id=" + name + " name=" + name + ">"); foreach (var item in selectList) { output.Append("<option value=" + item.Value.ToString() + ">" + item.Text + "</option>"); } output.Append("</select>"); return MvcHtmlString.Create(output.ToString()); } // 3rd sort method: pass in order by parameter - how do I use this? public static IHtmlString DropDownListSorted(this HtmlHelper helper,SelectList selectList,string orderBy) { StringBuilder output = new StringBuilder(); //How do I use the orderBy parameter? (selectList).OrderBy(l => l.Text); output.Append("<select id=" + name + " name=" + name + ">"); foreach (var item in selectList) { output.Append("<option value=" + item.Value.ToString() + ">" + item.Text + "</option>"); } output.Append("</select>"); return MvcHtmlString.Create(output.ToString()); } } } 我真的不知道最好的方法,可能有一个更简单的方式,我完全失踪,我可能会在我不能再看到树木的地方.一些问题 >我应该返回一个SelectList或MvcHtmlString,还是其他完全? 如果有人有一些想法或建议,那么我会感激一些反馈:) 解决方法
代码的第一个也是最重要的部分是摆脱任何ViewBag / ViewData(我个人认为是MVC应用程序的癌症)并使用视图模型和强类型视图.
因此,让我们首先定义一个视图模型,该模型将表示我们的视图将使用的数据(在此示例中为dropdownlistg): public class MyViewModel { public string SelectedItem { get; set; } public IEnumerable<SelectListItem> Items { get; set; } } 然后我们可以有一个控制器: public class HomeController : Controller { public ActionResult Index() { var model = new MyViewModel { // I am explicitly putting some items out of order Items = new[] { new SelectListItem { Value = "5",Text = "Item 5" },new SelectListItem { Value = "1",Text = "Item 1" },new SelectListItem { Value = "3",Text = "Item 3" },new SelectListItem { Value = "4",Text = "Item 4" },} }; return View(model); } } 和一个观点: @model MyViewModel @Html.DropDownListForSorted( x => x.SelectedItem,Model.Items,new { @class = "foo" } ) 最后一个是帮助方法,它将按值对下拉列表进行排序(您可以根据文本对其进行排序): public static class HtmlExtensions { public static IHtmlString DropDownListForSorted<TModel,TProperty>( this HtmlHelper<TModel> helper,Expression<Func<TModel,TProperty>> expression,IEnumerable<SelectListItem> items,object htmlAttributes ) { var model = helper.ViewData.Model; var orderedItems = items.OrderBy(x => x.Value); return helper.DropDownListFor( expression,new SelectList(orderedItems,"Text"),htmlAttributes ); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 迁移到WebApi 2.2 RC并获取响应状态406(不可
- asp.net-core – 来自带有Entity Framework Core的Data Tie
- C#_.NetFramework_Web项目_EXCEL数据导出 C#_.N
- asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadMod
- 为什么在ASP.NET中刷新页面时会执行按钮单击事件?
- asp.net-mvc – ASP.NET MVC(4) – 按特定顺序绑定属性
- asp.net-mvc – ASP MVC3在actionlink中插入html标签
- asp.net-mvc – 在nopcommerce 2.8中使用Telerik插件的自定
- asp.net-mvc – 如何在asp.net mvc 3中使用@html.actionlin
- C#_.net core 3.0自定义读取.csv文件数据_解决首行不是标题