User-1741097413 posted
I have this situation, where multiple indexes share the same Edit
and Details view (indexes are users with different roles). So I need to redirect the Admin to the right index after posting an
Edit or getting data in Details. Edit
has submit and back
buttons for redirecting, while Details has
back button.
So, this is what I did, the extension:
namespace DL.SO.ViewToView.WebUI.Extensions
{
public static class HttpRequestExtension
{
public static string GetCurrentRelativeUrl(this HttpRequest request)
{
return $"{ request.Path }{ request.QueryString }";
}
}
}
In Edit button (I'm using one partial view for all the indexes)
<a asp-controller="User" asp-action="Edit" asp-route-id="@item.Id" asp-route-returnUrl="@Context.Request.GetCurrentRelativeUrl()">Edit</a>
<a asp-controller="User" asp-action="Details" asp-route-id="@item.Id" asp-route-returnUrl="@Context.Request.GetCurrentRelativeUrl()">Details</a>
In the view model I have this property:
public string ReturnUrl { get; set; }
That I recive it as a parameter in Get Edit:
[HttpGet]
public async Task<IActionResult> Edit(string id, string returnUrl)
and finally the Post Edit:
[HttpPost]
public async Task<IActionResult> Edit(EditUserViewModel model)
{
var user = await _userManager.FindByIdAsync(model.Id);
if (user == null)
{
return NotFound();
}
else
{
user.Name = model.Name;
if (Url.IsLocalUrl(model.ReturnUrl))
{
return Redirect(model.ReturnUrl);
}
}
return View(model);
}
The redirecting works fine because I have post action, but in the Details
View I tried to do it in the view like this:
@if (Model.ReturnUrl == "/User/IndexManager")
{
<a asp-controller="User" asp-action="IndexManager">Back to List</a>
//show some data
}
else
{
<a asp-controller="User" asp-action="Index">Back to List</a>
}
I don't like it this way and I don't think it a good approach, does it matter if its case-sensitive? Is it vulnerable to mistakes?
Regards,