網站首頁 編程語言 正文
我們知道,在ASP.NET MVC中實現多選Select的話,使用Html.ListBoxFor或Html.ListBox方法就可以。在實際應用中,到底該如何設計View Model, 控制器如何接收多選Select的選中項呢?
實現效果如下:
初始狀態某些選項被選中。
當按著ctrl鍵,進行重新選擇多項,點擊"提交"按鈕,把選中項的id拼接。
對于Select中的項,包含顯示值,Value值,以及是否選中,抽象成如下的類。
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
對于整個多選Select來說,在ASP.NET MVC中,所有的選項被看作SelectListItem類型,選中的項是一個string類型的集合。于是多選Select的View Model設計為:
public class CityVm
{
public IEnumerable<SelectListItem> Cities { get; set; }
public IEnumerable<string> SelectedCities { get; set; }
}
在HomeController中,把SelectListItem的集合賦值給CityVm的Cities屬性。
public class HomeController : Controller
{
public ActionResult Index()
{
var cities = new List<City>
{
new City(){Id = 1, Name = "青島", IsSelected = true},
new City(){Id = 2, Name = "膠南", IsSelected = false},
new City(){Id = 3, Name = "即墨", IsSelected = true},
new City(){Id = 4, Name = "黃島", IsSelected = false},
new City(){Id = 5, Name = "濟南", IsSelected = false}
};
var citiesToPass = from c in cities
select new SelectListItem() {Text = c.Name, Value = c.Id.ToString(),Selected = c.IsSelected};
CityVm cityVm = new CityVm();
cityVm.Cities = citiesToPass;
ViewData["cc"] = citiesToPass.Count();
return View(cityVm);
}
......
}
在Home/Index.cshtml中,是一個CityVm的強類型視圖,對于選中的項會以IEnumerable<string>集合傳遞給控制器。
@model MvcApplication1.Models.CityVm
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm("GetCities", "Home", FormMethod.Post, new {id = "myForm"}))
{
@Html.ListBoxFor(c => c.SelectedCities, Model.Cities, new {size = ViewData["cc"]})
<br/>
<input type="submit" value="提交"/>
}
在HomeController中,再把從視圖傳遞過來的IEnumerable<string>拼接并呈現。
public class HomeController : Controller
{
......
[HttpPost]
public ActionResult GetCities(IEnumerable<string> selectedCities)
{
if (selectedCities == null)
{
return Content("沒有選中任何選項");
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append("選中項的Id是:" + string.Join(",", selectedCities));
return Content(sb.ToString());
}
}
}
原文鏈接:https://www.cnblogs.com/darrenji/p/4377157.html
相關推薦
- 2022-11-13 Elasticsearch6.2服務器升配后的bug(避坑指南)_服務器其它
- 2022-09-04 使用Django+Pytest搭建在線自動化測試平臺_python
- 2022-04-27 Python?Pandas學習之基本數據操作詳解_python
- 2022-07-28 C++實例講解引用的使用_C 語言
- 2022-08-31 C語言詳解實現猜數字游戲步驟_C 語言
- 2022-09-16 Linux?Shell如何用ssh命令統計分布式集群信息詳解_linux shell
- 2022-10-27 React?State與生命周期詳細介紹_React
- 2022-04-24 Redis三種特殊數據類型的具體使用_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支