I'm trying to Redirect from view to action using Razor without clicking. Sometimes an error occurs when the page is loading.

I made a try and catch block to redirect.

View:

@{
    var x = Session["Username"];

    try
    {
        if (x.ToString().Equals(item.User.UserName))
        {
            <td>
                @Html.ActionLink("تعديل", "Edit", new { id = item.NotificationID }) |
                @Html.ActionLink("التفاصيل", "Details", new { id = item.NotificationID }) |
                @Html.ActionLink("حذف", "Delete", new { id = item.NotificationID })    
            </td>
        }
    }
    catch (Exception e)
    {
        Response.Redirect("~/Views/Home/Index.cshtml");
        //I want to redirect to an action, not view.
    }
}

Edit:

Home Controller:

[Authorize]
[AuthorizeUserAccessLevel(UserRole = "Manager,Reviewer")]
public class HomeController : Controller
{

    TheUnitOfEnternalAuditEntities db = new TheUnitOfEnternalAuditEntities();



    public ActionResult Index()
    {
        var username = User.Identity.Name;
        username = username.Replace("NCA\\", "");

        User user = db.Users.Where(u => u.UserName.Equals(username)).First();





        Session["UserName"] = username;
        Session["UserID"] = user.UserID;
        Session["UserFirstName"] = user.FirstName;
        Session["UserFullName"] = user.FullName;
        Session["UserRole"] = user.Role.RoleID;


        var Departments = db.Departments.ToList();

        ViewBag.Departments = Departments;


        return View(db.Departments.ToList());
    }
}

Any Suggestions?

Related posts

Recent Viewed