Ehsan Akbar
Ehsan Akbar

Reputation: 7301

Redirect to an action in controller doesn't work

I have this code :

 public ActionResult Create()
        {
            if (dbcontext.SettingConfs.First().offlineState == "true")
            {

                TempData["Error"] = "ژمان ثبت نام به پایان رسیده است  ";
                RedirectToAction("login", "Account");
                return View();

            }
            else if (User.IsInRole("Admin"))
            {
                return View("Create");

            }
            else if (dbcontext.SettingConfs.First().offlineState == "true")
            {
                return View("Create");
            }

            else
            {
                return View();
            }

        }

In the first if statement i checked the offlineState if it's true i should redirect the controller to account controller .but it doesn't work and the mvc return My view(i.e create view) but i need to redirect to login action in account controller.Where is the problem .?

My login view :

@model EducationMVC.Models.LoginModel

@{
    ViewBag.Title = "ورود به سامانه";
    Layout = "~/Views/Shared/_LayoutLogin.cshtml";
}


@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div id="wrapper-right">
         <div id="text-product"></div>
     </div>
    <div id="wrapper-left">
        <div id="wrapper-login">
            <div class="rows-input-login">

                <span style="font:normal 13px BYekan;color:#252525">
                    نام کاربری :
                </span>
                @Html.TextBoxFor(m => m.UserName, new {@class = "username"})
                <div style="float:left;font:normal 11px tahoma;color:#ea0000;padding-top:8px;padding-left:5px;margin-top:-85px">
                    @Html.ValidationMessageFor(m => m.UserName) 
                </div>
            </div>
            <div class="rows-input-login" style="margin-top:10px;">

                <span style="font:normal 13px BYekan;color:#252525">
                    رمز عبور : 
                </span>
                @Html.PasswordFor(m => m.Password)
                <div style="float:left;font:normal 11px tahoma;color:#ea0000;padding-top:8px;padding-left:5px;margin-top:-85px">
                    @Html.ValidationMessageFor(m => m.Password)
                </div>
            </div>

            <div class="rows-input-login" style="margin-top: 20px">
                <div id="row-right">
                    <div class="rows-input-login" style="height:30px;">
                        @if (TempData.ContainsKey("Message"))
                        {
                            <div class="wrapper-error" style="background: transparent; color: green">
                                @TempData["Message"]
                            </div>
                        }
                    </div>
                </div>
                <div id="row-left">
                    <input type="submit" value="ورود" class="button-caution"/>
                </div>
            </div>
            <div class="rows-input-login" style="margin-top:10px;">

                @if (TempData.ContainsKey("Error"))
                {
                    <div class="wrapper-error" style="background: transparent;font-size:11px; color: #ea0000">
                        @TempData["Error"]
                    </div>
                }
            </div>

        </div>
    </div>
}

best regards

Upvotes: 0

Views: 739

Answers (4)

Oscar Bralo
Oscar Bralo

Reputation: 1907

Try this:

return RedirectToAction("login", "Account");

I hope this helps

Upvotes: 1

SoftSan
SoftSan

Reputation: 2472

just write:

return RedirectToAction("login", "Account");

Upvotes: 1

nick shp
nick shp

Reputation: 90

 RedirectToAction("login", "Account");
                return View();

Change it to :

 return RedirectToAction("login", "Account");

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

change your if condition to this, you are not reutring RedirectToAction you are returning View thats the reason View is displayed instead of redirect, For redirecting you have ti write return RedirectToAction("Action","Controller"):

if (dbcontext.SettingConfs.First().offlineState == "true")
            {

                TempData["Error"] = "ژمان ثبت نام به پایان رسیده است  ";
                return RedirectToAction("login", "Account");

            }

For more understanding Visit this link

Upvotes: 2

Related Questions