在我的视图中使用@ html.labelfor和@foreach循环出错 [英] Error with @html.labelfor and @foreach loop in my view
问题描述
HI大家
i我是mvc的新手我面临一个问题,我有一个简单的视图,包含一个表单和一个foreach循环循环的语法很好,如果我这样做
HI everyone
i am new to mvc i am facing a problem, i have a simple view that contains a form and a foreach loop the syntax of the loop goes fine if i did this
@model IEnumerable<SearchBox.Models.Students>
但是labelfor的语法给我错误,如果我这样做了
but the syntax of labelfor give me error and if i did this
@model SearchBox.Models.Students
到我的视图然后foreach循环模型用红线下划线
我将要做什么来解决我的问题问题请帮助我提前退出感谢谢谢
我尝试过的事情:
这是查看
to my view then the foreach loop Model is underlined with a red line
What i will hhave to do to resolve my problem kindly help me to get out of it thankx in advance
What I have tried:
Here is View
@model IEnumerable<SearchBox.Models.Students>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.EditorFor(model => model.Name, new { htmlattributes = new { @class = "form-control" } })
</div>
</div>
</div>
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
控制器
Controller
public class DefaultController : Controller
{
private static List<Students> students = new List<Students>()
{
new Students() {Id = 1, Name = "Student - 1"},
new Students() {Id = 2, Name = "Student - 2"},
new Students() {Id = 3, Name = "Student - 3"},
new Students() {Id = 4, Name = "Student - 4"},
new Students() {Id = 5, Name = "Student - 5"},
new Students() {Id = 6, Name = "Student - 6"},
new Students() {Id = 7, Name = "Student - 7"},
new Students() {Id = 8, Name = "Student - 8"},
new Students() {Id = 9, Name = "Student - 9"}
};
// GET: Default
public ActionResult Index()
{
return View(students);
}
}
推荐答案
声明
The declaration
@model IEnumerable<SearchBox.Models.Students>
告诉视图,提供的数据是一种类型为Students的集合 - 这可以在循环中使用。
声明
tells the view, that the provided data is a kind of collection of type Students - this can be used in a loop.
The declaration
@model SearchBox.Models.Students
告诉视图,只有一个类型为student的对象(而不是集合),因此需要循环。
tells the view that there is only a single object of type students (instead of a collection) and therefor there is noting to loop through.
但我如何在我的视图中使用它们。
but how i can use both in my view.
它应该在这种方式,如果你只需要从一个模型展示
it shall be done in this way if you need to show from only one model
<div class="form-group">
@Html.LabelFor(model => model.First().Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.EditorFor(model => model.First().Name, new { htmlattributes = new { @class = "form-control" } })
</div>
</div>
否则使用此 MVC中单视图中的多个模型 [ ^ ]
这篇关于在我的视图中使用@ html.labelfor和@foreach循环出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!