Controller returning HTML to View is wrong?

14

I used this solution because when trying to render a table in a view with many records the user's browser gave Crash, I know it's a not very elegant solution but it was the only solution that came to mind. (In this view I have to load all records on the screen to the user)

I created a GenerateTable method where I populate with the database's data and return a String with HTML containing the data. In the View call I make an Ajax request that assembles the user table, basically I render it on the server side.

What are the disadvantages of using this template and what alternative could be used? 1

Prototype:

    

asked by anonymous 27.08.2014 / 14:07

3 answers

15

I would use another solution.

Reason: Mounting HTML is View's responsibility. The Controller must process the requests and execute the logic (to retrieve information, change the database, etc ...).

Following this idea I would not feel good putting HTML in the Controller, I do not know if the implementation and maintenance of this would be nice, even if I did not use that kind of solution that you chose.

Disadvantage of using this template: One of the premise of Asp.net Mvc is the clear separation of your business logic from your UI code, but depending on your implementation, you may end up mixing your business logic with the HTML you're generating on the Controller, walking a little against this idea.

Normally when it is necessary to load many records in the View I use a paging feature so you do not need to load all your data.

If I could not use paging , I would review / refactor / rethink the code. An alternative would be to create a HTML Helper to mount this table for example, but would not mount HTML on the Controller.

    
27.08.2014 / 14:32
2

You can have the same effect using a PartialView. I believe that if your project is object oriented, with a well-defined Model, it can help a lot. _Partials can be created in the same folder as your Views and you can add them right-click on the folder, Add, View and check the Create a partial view checkbox.

Controller

[HttpGet]
public ActionResult Detalhar()
{
    var suaLista = _suaBll.Detalhar();
    return PartialView("_Detalhamento", suaLista);
}

PartialView

@model SeuNameSpace.ModelDaSuaLista

<table>
    <tr>
        <th>@Html.LabelFor(m => @Model[0].Propriedade)</th>
    </tr>
    @foreach (var item in @Model)
    {
        <tr>
            <td>@Html.DisplayFor(m => item.Propriedade)</td>
        </tr>
    }
</table>

View

<div id="detalhamentoDiv" style="display: none; min-height: 80px;" data-request-url="@Url.Action("Detalhar", "SeuController")"></div>

Javascript

var detalhamentoDiv = $("#detalhamentoDiv");
var url = detalhamentoDiv.data('request-url');
detalhamentoDiv.load(url);
detalhamentoDiv.fadeIn(500);
    
08.09.2014 / 19:12
0

If this view is only viewed by the user (without printing) I do not recommend uploading too much data, after all, it will have to check the information there. If it wants to do a much more complete search, use tabs, loading parts of the table per se, so it becomes less messy for the user to search for information ...

    
10.09.2014 / 18:54