Мне нужен совет, прежде чем у меня разовьется дурная привычка.

У меня есть контроллер под названием 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);
}

Как вы можете заметить, они оба вызывают одно и то же представление ( это действие называется «вызвать представление». Каково его собственное имя?) .

@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>

Они оба являются наследниками одной и той же Модели.

У меня вопрос, правильно ли я делаю все? Или это просто хак, который мне удалось наскрести. Помогите мне, прежде чем я выучу дурную привычку.

6
задан ahsteele 23 January 2012 в 04:17
поделиться