asp.net-mvc – 在我养成一个坏习惯之前需要建议
发布时间:2020-12-16 03:53:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个名为AuctionsController的控制器.在其中我有一些名为Index()和AuctionCategoryListing()的动作: //Used for displaying all auctions.public ActionResult Index(){ AuctionRepository auctionRepo = new AuctionRepository(); var auctions = auct
我有一个名为AuctionsController的控制器.在其中我有一些名为Index()和AuctionCategoryListing()的动作:
//Used for displaying all auctions. public ActionResult Index() { AuctionRepository auctionRepo = new AuctionRepository(); var auctions = auctionRepo.FindAllAuctions(); return View(auctions); } //Used for displaying auctions for a single category. public ActionResult AuctionCategoryListing(string categoryName) { AuctionRepository auctionRepo = new AuctionRepository(); var auctions = auctionRepo.FindAllAuctions() .Where(c => c.Subcategory.Category.Name == categoryName); return View("Index",auctions); } 正如您所知,它们都调用相同的View(此动作称为“调用视图”.它的名称是什么?). @model IEnumerable<Cumavi.Models.Auction> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New","Create") </p> <table> <tr> <th></th> <th> IDSubcategory </th> <th> IDCity </th> <th> IDPerson </th> <th> Title </th> <th> TextBody </th> <th> ContactNumber </th> <th> AskingPrice </th> <th> AddressDirection </th> <th> LatestUpdateDate </th> <th> VisitCount </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.ActionLink("Edit","Edit",new { id=item.ID }) | @Html.ActionLink("Details","Details",new { id=item.ID }) | @Html.ActionLink("Delete","Delete",new { id=item.ID }) </td> <td> @item.IDSubcategory </td> <td> @item.IDCity </td> <td> @item.IDPerson </td> <td> @item.Title </td> <td> @item.TextBody </td> <td> @item.ContactNumber </td> <td> @String.Format("{0:F}",item.AskingPrice) </td> <td> @item.AddressDirection </td> <td> @String.Format("{0:g}",item.LatestUpdateDate) </td> <td> @item.VisitCount </td> </tr> } </table> 它们都继承了同一个模型. 我的问题是,我是否以正确的方式做事?或者这只是一个黑客,我设法凑到一起.在我学习坏习惯之前帮助我. 解决方法
我将其修改为:
public ActionResult Index(string categoryName) { AuctionRepository auctionRepo = new AuctionRepository(); var auctions=auctionRepo.FindAllAuctions(); if (!string.IsNullOrEmpty(categoryName)) { auctions = auctions.Where(c => c.Subcategory.Category.Name == categoryName); } return View(auctions); } 您的路线可能如下所示: context.MapRoute( "auction_defalt","Auction/{categoryName}",new { controller="Auction",action = "Index",categoryName = UrlParameter.Optional } 由于行动非常相似,我认为没有理由将它们分开. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- asp.net-mvc – 将IQueryable泛型转换为JSON
- ASP.Net MissingMethodException – 找不到“cto
- asp.net-mvc-3 – 谷歌页内分析在我的ASP.NET MV
- asp.net – UpdatePanel没有名为’TextBox’的公
- 使用带有asp.net.No错误消息的SalesLogix使用Ole
- asp.net-mvc – 我应该把我的控制器业务逻辑放在
- asp.net-mvc – ASP.NET MVC – 如何获取一个URL
- 如果我应该开始使用asp.net mvc 4而不是asp.net
- 将搜索框添加到ASP.Net动态数据列表页面
- asp.net-mvc – 如何绑定从linq到sql asp.net mv
热点阅读