0
votes

So i have made a partialview where users can sign in.

But when i submit forms i regular views i get an error "Child actions are not allowed to perform redirect actions."

This i because my [HttpPost] controller action in the controller that handles the partial view redirects and thats a NoGo apparently.

I dont get the error when a user logs in. To clarify when the button in the partial view is pressed there is no problem.

The reason for puting the login in a Partialview is because a user should be able sign in from any page in the application

Here Is the code:

Controller for partialview Credentials:

public ActionResult Identify() {
        if(Session["User"] != null) {
            return PartialView("Identified", Session["User"]);
        } else {
            return PartialView();
        }
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Identify(Login login) {
        if(ModelState.IsValid) {
            Authenticate(login.LoginName, login.LoginPasscode);
        }
        return Redirect(Request.UrlReferrer.AbsolutePath);
    }


@model ViewModels.Login

@using(Html.BeginForm("Identify", "Credentials")) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.LoginName)
            @Html.EditorFor(model => model.LoginName)
            @Html.ValidationMessageFor(model => model.LoginName)

            @Html.LabelFor(model => model.LoginPasscode)
            @Html.EditorFor(model => model.LoginPasscode)
            @Html.ValidationMessageFor(model => model.LoginPasscode)

            <input class="button" type="submit" value="Log Ind" />

    </div>

}

I have added the partial view by using the following line in _Layout

    @{Html.RenderAction("Identify", "Credentials");}

HomeController:

  [HttpGet]
    public ActionResult Index() {
        db.Role.ToList();
        return View();
    }

    [HttpPost]
    public ActionResult Index(string Foo) {
        db.Role.ToList();
        ViewBag.InTheBox = Foo;
        return View();
    }

and last home index view:

@if (ViewBag.InTheBox == null){
    ViewBag.InTheBox = "Nothing In Here";
}

<h1>@ViewBag.InTheBox</h1>    

@using (Html.BeginForm("Index","Home")) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <input type="text" name="Foo"/>
    <input type="submit" value="Submit" />
}

edit:

So i figured out how to solve the problem and it was really simple. My overloaded method Identify should only have a httpget variant. The httppost variant of the method was renamed in this case to Authenticate and the form posts to that method. This resolved the problem. Correct code below.

Controller for partialview Credentials:

public ActionResult Identify() {
        if(Session["User"] != null) {
            return PartialView("Identified", Session["User"]);
        } else {
            return PartialView();
        }
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Authenticate(Login login) {
        if(ModelState.IsValid) {
            Authenticate(login.LoginName, login.LoginPasscode);
        }
        return Redirect(Request.UrlReferrer.AbsolutePath);
    }


@model ViewModels.Login

@using(Html.BeginForm("Authenticate", "Credentials")) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.LoginName)
            @Html.EditorFor(model => model.LoginName)
            @Html.ValidationMessageFor(model => model.LoginName)

            @Html.LabelFor(model => model.LoginPasscode)
            @Html.EditorFor(model => model.LoginPasscode)
            @Html.ValidationMessageFor(model => model.LoginPasscode)

            <input class="button" type="submit" value="Log Ind" />

    </div>

}

1
I'm not overly familiar with razor but with a django MVC app I would have that type of logic exist inside of a masterpage that all pages inherit from. - Chris Hawkes
I am not sure how to translate that to my particular problem - user2726213

1 Answers

0
votes

Try

RedirectToAcion(myActionName).