加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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 }

由于行动非常相似,我认为没有理由将它们分开.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读