MrNutbutters
MrNutbutters

Reputation: 17

StackOverflowException is caused by a default mvc view

I created a simple create view for a model by mvc's automated way to show to a coleague. Now, although i do not really use that, i can't see where a recursion is caused since i just said to mvc to create it for a model where all the fields are made with the "public 'type' 'name' { get; set;}" format. I will include the model, action, and view below. This isn't really a problem for me since i do not use the templates at all but I am curious how this could happen when no custom code was inserted

Thanks in advance

Controller Action

public ActionResult View()
        {
            return View();
        }

Model

namespace Data_Access.Models
{
    public class StudentModel
    {
        public int studentId { get; set; }
        public string name { get; set; }
        public string surname { get; set; }
        public string classroom { get; set; }
        public string role { get; set; }
        public string imgPath { get; set; }
    }
}

And finally the View

@model Data_Access.Models.StudentModel

@{
    ViewBag.Title = "View";
}

<h2>View</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>StudentModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.studentId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.studentId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.studentId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.surname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.surname, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.surname, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.classroom, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.classroom, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.classroom, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.role, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.imgPath, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.imgPath, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.imgPath, "", 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>

Upvotes: 0

Views: 172

Answers (2)

Aixasz
Aixasz

Reputation: 448

You can change action name to ViewPage() and add route attribute Route[("View")] above the action.

example:

[Route("View")]
public IActionResult ViewPage()
{
    return View();
}

then rename View.cshtml to ViewPage.cshtml

This will prevent recursive and you also use url path /View

I hope this will help you.

Upvotes: 1

VillageTech
VillageTech

Reputation: 1995

The controller has infinite recursion:

public ActionResult View()
{
    return View();
}

Your View() method calls the same View() method ;) Probably naming problem - I can't suggest any solution here, because you didn't take any information about the flow, what should be implemented in controller.

Upvotes: 3

Related Questions