StackOverflowException 是由默认的 mvc 视图引起的
StackOverflowException is caused by a default mvc view
我通过 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>
控制器有无限递归:
public ActionResult View()
{
return View();
}
您的 View() 方法调用相同的 View() 方法 ;)
可能是命名问题——我不能在这里建议任何解决方案,因为你没有获取任何关于流程的信息,应该在控制器中实现什么。
您可以将动作名称更改为ViewPage()
,并在动作上方添加路由属性Route[("View")]
。
示例:
[Route("View")]
public IActionResult ViewPage()
{
return View();
}
然后将 View.cshtml
重命名为 ViewPage.cshtml
这将防止递归并且您还使用 url 路径 /View
希望对您有所帮助。
我通过 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>
控制器有无限递归:
public ActionResult View()
{
return View();
}
您的 View() 方法调用相同的 View() 方法 ;) 可能是命名问题——我不能在这里建议任何解决方案,因为你没有获取任何关于流程的信息,应该在控制器中实现什么。
您可以将动作名称更改为ViewPage()
,并在动作上方添加路由属性Route[("View")]
。
示例:
[Route("View")]
public IActionResult ViewPage()
{
return View();
}
然后将 View.cshtml
重命名为 ViewPage.cshtml
这将防止递归并且您还使用 url 路径 /View
希望对您有所帮助。