Flame_Phoenix
Flame_Phoenix

Reputation: 17584

Make huge view in C# MVC5 maintanable

I have an application that is using MVC5 with C# and Razor Engine. This view displays a huge table:

<table class="table table-bordered" id="pendingTable">
    <thead>
        <tr>
            <th>Actions</th>
            <th>Order Status</th>
            <th>Order Details</th>
            <th>Order Date</th>
            <th>Expected Delivery Date</th>
            <th>Ordered By</th>
            <th>Employee ID</th>
            <th>Employee Status</th>
            <th>Employee Name</th>
            <th>Employee Type</th>
            <th>Scope</th>
            <th>Delivery Address</th>
            <th>Comment</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td> <!-- At least 8 TDs each one with hundreds of lines of code-->
        </tr>
    </tbody>
</table>

Now, the problem is that everything is in one huge file. As you can guess, this file is a nightmare to update and maintain.

So I am familiar with the C# region directive, but I can't find anything simillar for Views. I also know about partial views, but I have the strong impression from discussions in StackOverflow that these should only be used when I have a piece of code in a View that is re-usable, which is not the case.

What is the best way to deal with Views this large?

Upvotes: 1

Views: 337

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

Using a PartialView is the correct approach here.

Even when used in just only one place, PartialView is useful for achieving separation of concerns and like your case seems to be here, useful segregation of big files into smaller ones which are easier to read and maintain.

However, if you end up having serious logic in your View, consider creating a ViewModel class where you process that logic.

For example you can have different ViewModels for different user types. Subsequently you can use separate PartialViews to render information about different user types.

Now this is assuming that you are looking for a cheap solution. In case you have the time and will to look into more complex and effective solutions, follow Stephen Muecke's advice in the comments. He correctly points out that the best approach is to use EdiorTemplate and/or custom HtmlHelper.

Upvotes: 1

Related Questions