0

我通过 mvc 的自动化方式为模型创建了一个简单的创建视图,以向同事展示。现在,虽然我并没有真正使用它,但我看不到递归是在哪里引起的,因为我只是对 mvc 说要为一个模型创建它,其中所有字段都是用 "public 'type' 'name' { get ; 设置;}" 格式。我将在下面包含模型、操作和视图。这对我来说不是问题,因为我根本不使用模板,但我很好奇在没有插入自定义代码时会发生这种情况

提前致谢

控制器动作

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

2 回答 2

3

控制器具有无限递归:

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

您的 View() 方法调用相同的 View() 方法;) 可能是命名问题 - 我不能在这里提出任何解决方案,因为您没有获取有关流程的任何信息,应该在控制器中实现什么。

于 2019-12-02T09:37:26.103 回答
1

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

例子:

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

然后重命名View.cshtmlViewPage.cshtml

这将防止递归,您还可以使用 url 路径/View

我希望这能帮到您。

于 2019-12-02T09:49:34.120 回答