razor – 使用ActionLink将null参数传递给控制器
发布时间:2020-12-16 07:19:02 所属栏目:asp.Net 来源:网络整理
导读:我有一个列出一堆类别的视图: ul li@Html.ActionLink("All","Index","Products")/li @foreach (var item in Model.ProductCategories) { li@Html.ActionLink(item.Name,new { id = item.Id },null)/li }/ul 如图所示,我应该得到一个类别链接列表,最上面的一
我有一个列出一堆类别的视图:
<ul> <li>@Html.ActionLink("All","Index","Products")</li> @foreach (var item in Model.ProductCategories) { <li>@Html.ActionLink(item.Name,new { id = item.Id },null)</li> } </ul> 如图所示,我应该得到一个类别链接列表,最上面的一个是“All”,下面的是相应的类别名称,id传递给控制器??. 我的控制器看起来像这样: public ActionResult Index(int? id) { var categories = _productCategoryRepository.GetAll().OrderByDescending(f => f.Name); var items = id == null ? _productItemRepository.GetAll().OrderBy(f => f.Name).ToList() : _productCategoryRepository.GetSingle((int)id).ProductItems.OrderBy(f => f.Name).ToList(); var model = new ProductsViewModel() { ProductCategories = categories,ProductItems = items }; return View(model); } 所以类别应该始终相同.但是当id为null时,项应显示每个项,当设置id时,项应显示特定类的项. 这一切都非常好,所以当我点击一个类别链接.我这样得到了网址: /产品/首页/ 3 大!现在我点击“全部”链接,但它将我路由到/ Products / Index / 3,即使我显然没有传递参数.我尝试传递一个空值: @Html.ActionLink("Alle","Products",new { id = null }) 但我得到错误:无法将’null’分配给匿名类型属性. 如何强制null传递给我的索引控制器? 解决方法
你的控制器动作会很高兴接受一个可以为空的int,所以给它一个可以为空的int就像这样!
@Html.ActionLink("Alle",new { id = (int?)null }) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET Core和Angular的RDLC本地报表查看器(> 2.0)
- asp.net-mvc – 在WebApi的ActionFilterAttribute的OnActio
- asp.net – imagebutton onclientclick和onclick事件
- 处理asp:ScriptManager时,mscorlib.dll中的asp.net – Exe
- .net – 具有流畅验证的Ninjects
- asp.net – 我仍然犹豫使用ViewModels而不是View for Model
- asp.net – 如何修改aspx页面的超时?
- .net – 不能使用moles来模拟MVC框架
- asp.net-mvc – 无法在Web服务器上启动调试. Web服务器找不
- ASP.Net Web API架构选择