StackOverflowException由默认的mvc视图引起

问题描述 投票:0回答:2

我通过mvc的自动方式为同事创建了一个简单的模型创建视图。现在,尽管我并没有真正使用它,但我看不出是在哪里引起递归的,因为我只是对mvc说,要为模型创建模型,其中所有字段都由“ public'type''name'{get; set;}”格式。我将在下面包括模型,操作和视图。这对我来说不是真正的问题,因为我根本不使用模板,但是我很好奇在没有插入自定义代码的情况下会如何发生]

提前感谢

控制器动作

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

型号

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; }
    }
}

最后是视图

@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>

我通过mvc的自动方式为同事创建了一个简单的模型创建视图。现在,尽管我并没有真正使用它,但是由于我只是对mvc说......>

c# asp.net-mvc stack-overflow defaultview
2个回答
2
投票

控制器具有无限递归:

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

0
投票

您可以将动作名称更改为ViewPage(),并在动作上方添加路径属性Route[("View")]

© www.soinside.com 2019 - 2024. All rights reserved.