AndreaNobili
AndreaNobili

Reputation: 43057

What can I do to come back to this previous view in C#/.NET?

I am pretty new in C# and .NET MVC framework and I have the following problem.

I have a first JQuery Mobile view that show a navbar containing some tabs. Into one of this tab I putted a ListView that show the element of a collection of DataModel.Vulnerability.Fix objects represented by the Model.VulnerabilityFixes into my model object. On the right of every element of the list I have put a button/link to delete the related Fix object that generate this row in the list.

This work fine and I obtain the following result (I post a screenshot):

enter image description here

This is the code of the previous tab (the one that show the Fix list):

<!-- TAB-2: FIXES, SOLUTION e MITIGATING STRATEGY: -->
  <div id="tab-2" class="ui-body-d ui-content">

    <h3>Fixes</h3>

    <a href="@Url.Action("SearchCPE", "Asset", new { id = Model.Id })"  data-icon="plus"  data-inline="true" data-mini="true"  data-role="button"  >Aggungi un Fix</a>

    <!-- Tabella contenente la lista delle fix: -->
    <ul data-role="listview" data-inset="true" data-theme="b" data-split-icon="delete">
        @foreach (DataModel.Vulnerability.Fix item in Model.VulnerabilityFixes)
        {


            <li><a href="@Url.Action("Details", "Product", new { Title = item.Title })">
                <h2>@item.Title</h2>
                <table style="width: 100%">
                    <tr>
                        <th>Id</th>
                        <th>FixName</th>
                        <th>Vendor</th>
                        <th>Version</th>
                    </tr>
                    <tr>
                        <td>@MyHelper.decodeNull(item.Id)</td>
                        <td>@MyHelper.decodeNull(item.FixName)</td>
                        <td>@MyHelper.decodeNull(item.Vendor)</td>
                        <td>@MyHelper.decodeNull(item.Version)</td>
                    </tr>
                </table>

            </a>
                <a href="@Url.Action("DeleteFix", "Editing", new { vulnId = Model.Id, currentFixId = item.Id, currentFixName = item.FixName })">Delete</a>

            </li>
        }
    </ul>
  </div>
 <!-- /tab-2 -->

Ok, now I have a DeleteFix() method into the EditingController class that handle the request generated clicking od the Delete button.

This one:

    public ActionResult DeleteFix(long vulnId, int currentFixId, string currentFixName) 
    {

        DataModel.Vulnerability.Fix model = new DataModel.Vulnerability.Fix();

        manager.openConnection();

        try
        {
            model.Id = currentFixId;
            model.FixName = currentFixName;
        }
        finally
        {
            manager.closeConnection();
        }

        return View(model);

    }

This method show another view, the DeleteFix.cshtml file, that show a confirm window where is asked to the user to confirm the delete operation or if come back to the previous page, this is the code:

@model DataModel.Vulnerability.Fix

@{
    ViewBag.Title = "DeleteFix";
    Layout = "~/Views/Shared/MasterPageMobile.cshtml";
}

<h1>Delete Fix</h1>

<h2>Fix: @Model.Title (id: @Model.Id)</h2>

<p>
    Confermare la cancellazione del fix "@Model.FixName" ?
</p>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <input type="hidden" name="id" value ="@Model.Id" />

    <div data-role="controlgrup" data-type="horizontal" data-mini="true">
        <a href="@Url.Action("Index", "Editing", new { id = Model.Id })"   data-inline="true"  data-mini="true"  data-role="button"  >Torna alla lista</a>
        <a href="@Url.Action("Details", "Groups", new { id = Model.Id })"   data-mini="true"  data-inline="true"   data-role="button"  >Annulla</a>

        <input type="submit" value="Delete" data-mini="true" data-inline="true" />
    </div>
}

My problem is that I want that if the user click on the Torna alla lista button it have to be taken to the initial view that show the list of Fix object but I can't do it

Someone can help me to understand what am I missing? What can I do to obtain this result?

Tnx

Upvotes: 0

Views: 99

Answers (1)

boblemar
boblemar

Reputation: 1153

Basically I would add a new property in the viewmodels used in the secondary views to have a trace of the primary url :

public string BackUrl { get; set; }

Maybe if you want this feature for more that I secondary view, you can create a base view model that all secondary viewmodels inherit.

Then, when calling a secondary view, just initialize the BackUrl property :

<a href="@Url.Action("DeleteFix", "Editing", new { vulnId = Model.Id, currentFixId = item.Id, currentFixName = item.FixName, BackUrl = @request.RawUrl })">Delete</a>

In the end, in the DeleteFix action, redirect to the BackUrl instead of redering the view (if the viewstate is valid :

public ActionResult DeleteFix(long vulnId, int currentFixId, string currentFixName) 
{
    if (ModelState.IsValid)
    {
        DataModel.Vulnerability.Fix model = new DataModel.Vulnerability.Fix();

        manager.openConnection();

        try
        {
            model.Id = currentFixId;
            model.FixName = currentFixName;
        }
        finally
        {
            manager.closeConnection();
        }

        return Redirect(viewModel.BackUrl);
    }

    // Invalid viewstate, re-render the view
    return View(model);
}

Upvotes: 1

Related Questions