养成不良习惯之前

养成不良习惯之前

我有一个名为AuctionsController的 Controller 。在其中有名为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(此操作称为“调用 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 }

由于这些 Action 是如此相似,因此我看不出将它们分开的理由。

关于asp.net-mvc - 在养成不良习惯之前需要咨询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4442844/

10-09 08:25