Ben Hoffman
Ben Hoffman

Reputation: 8259

Non-Model Driven Return page in MVC

I am creating my first MVC website. On the site, I need to create a page that allows a user to choose from a object stored in my model database, enter some additional data (in text fields), and then click a button and do a calculation returning a table of results. These results will not be stored in the database. Also, this table of data could change in the number of columns etc that are turned based on what object is chosen.

What is the best way to do this with MVC? It is not a standard CRUD operation and all of the MVC tutorials I have research only really cover CRUD operations.

Upvotes: 0

Views: 120

Answers (2)

Andre Calil
Andre Calil

Reputation: 7692

ASP.NET MVC pages can be strongly typed to any class (including .Net classes) or non-typed at all, that's not big deal.

In your place, I'd use a DataTable object (from System.Data). You could also sign a struct or pass a list of dictionaries to the view, but lets be practical.

Your action (at the controller):

[HttpPost]
public ActionResult DoMath(int yourArguments)
{
    System.Data.DataTable yourTabularResults = new System.Data.DataTable();

    //your magic

    return View(yourTabularResults);
}

And your view:

@model System.Data.DataTable

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>DoMath</title>
</head>
<body>
    <div>
        @foreach (System.Data.DataRow currentLine in Model.Rows)
        {
            //do your job
        }
    </div>
</body>
</html>

Regards

Upvotes: 1

Eric J.
Eric J.

Reputation: 150118

In your View, simply generate the desired HTML code. In the following, I assume SelectedObject is the object your user selected to be rendered as a table.

<table>
@for (int row = 0; row < SelectedObject.Rows; row++) {
    <tr>
    @for (int col = 0; col < SelectedObject.Columns; col++) {
        <td>@SelectedObject.GetDataFor(row, col)</td>
    @}

    </tr>
@}
</table>

Upvotes: 1

Related Questions