question

AsistanceRequest-0783 avatar image
0 Votes"
AsistanceRequest-0783 asked AsistanceRequest-0783 commented

preventing the automatic redirect on a view when running

i nested my create page into my index view and im pretty sure the "Child actions are not allowed to perform redirect actions" error im getting is due to that. when i press the create button inside of the create view i get no error.

how do i make it so the create button doesnt try to redirect?

dotnet-aspnet-core-mvcdotnet-aspnet-mvc
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AsistanceRequest-0783,

Can you post the relate code (Index View and Create page)? It is easier for us to reproduce the problem and help you fix it.

0 Votes 0 ·

i was able to replicate the error with auto-generated code. all you need to do is make a normal mvc program, go to index, add @Html.Action("Create") to it, run, input relevant data, and press create. you should get the same error as me

0 Votes 0 ·
ZhiLv-MSFT avatar image ZhiLv-MSFT AsistanceRequest-0783 ·

Hi @AsistanceRequest-0783,

I want to confirm that your application is a traditional MVC application, right? As far as I know, the @Html.Action() is used in the traditional .NetFramework MVC application, instead of the Asp.net Core Application. If that is the case, I will create a traditional MVC application and test it.

0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
1 Vote"
ZhiLv-MSFT answered AsistanceRequest-0783 commented

Hi @AsistanceRequest-0783,

"Child actions are not allowed to perform redirect actions" error

This is not allowed because MVC has already started Rendering the View to the browser (client). So, the MVC Frameworks blocks this action because the client already receives data (html). As long as the rendering is in progress you not able to redirect in your child view.

i nested my create page into my index view

To avoid the above error and add the Create page content in the Index view, I suggest you could use a Partial View to add the new item.

For example: Here I create an Employees Controller and use scaffolding generate the related Views.

In the Views/Employees folder, add a Create_PV.cshtml partial view, with the following code (the partial view content is similar with the Create page content, in the partial view, we should set submit action parameters: action name, controller name and the form method):

 @model NetMVC.Models.Employee
  @using (Html.BeginForm("Create","Employees",FormMethod.Post))   //when click the submit button, submit the form to Create action method.
 {
     @Html.AntiForgeryToken()
        
     <div class="form-horizontal">
         <h4>Employee</h4>
         <hr />
         @Html.ValidationSummary(true, "", new { @class = "text-danger" })
         <div class="form-group">
             @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })
             <div class="col-md-10">
                 @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })
                 @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" })
             </div>
         </div>
    
         <div class="form-group">
             @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
             <div class="col-md-10">
                 @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
                 @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
             </div>
         </div>
    
         <div class="form-group">
             @Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" })
             <div class="col-md-10">
                 @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
                 @Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
             </div>
         </div>
    
         <div class="form-group">
             <div class="col-md-offset-2 col-md-10">
                 <input type="submit" value="Create" class="btn btn-default" />
             </div>
         </div>
     </div>
 }
    
 <div>
     @Html.ActionLink("Back to List", "Index")
 </div>
    
 @section Scripts {
     @Scripts.Render("~/bundles/jqueryval")
 }

Then, in the Index page, use the following code to render the partial view.

 @using NetMVC.Models; //add the model reference, NetMVC is the application name

 @Html.Partial("Create_PV", new Employee())

The result as below:

111919-10.gif


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,
Dillion


10.gif (1.7 MiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thank you! i had tried a partial view before i made this topic but i wasnt properly implementing it so i thought that wasnt good enough.

0 Votes 0 ·