您的位置:首页 > Web前端 > HTML

@Html.EditorForModel()用法

2016-11-03 23:08 369 查看
MSDN上对@Html.EditorForModel()的解释是Returns an HTML input element
for each property in the model, using additional view data, 即使用另外一个View来为本VIew中Model每一个属性返回一个Html 输入元素。

以Student类为例,实现显示出数据库表中所有的学生信息的结果,具体用法如下:

1, 在Student文件夹中的Index.cshtml中放入@Html.EditorForModel(),代码如下:

@model IEnumerable<MVC_DeleteMultiRow.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Employee List</h2>
@using (Html.BeginForm("Delete", "Student", FormMethod.Post))
{
<table border="1">
<thead>
<tr>
<th>
<input type="checkbox" name="checkAll" id="checkAll">
</th>
<th>
Name
</th>
<th>
Gender
</th>
<th>
Email
</th>
</tr>
</thead>
<tbody>
<span style="background-color: rgb(255, 255, 51);"> <span style="background-color: rgb(255, 255, 102);">@Html.EditorForModel()<</span>/span>
</tbody>
</table>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Delete Selected students"/>
}


2,在View文件夹下的Shared文件夹下新建一个文件夹EditorTemplates,在EditorTemplates文件夹中新建一个View,文件名为Student.cshtml,在此文件中放入想显示的html元素及model的属性,本例的代码如下:

@model MVC_DeleteMultiRow.Models.Student
<h2>Student</h2>
<tr>
<td>
<input type="checkbox" name="employeeIdsToDelete" id="employeeIdsToDelete" value="@Model.ID">
</td>
<td>
@Model.Name
</td>
<td>
@Model.Gender
</td>
<td>
@Model.Email
</td>
</tr>
这样就可以实现我们想要的效果了,文件夹目录以及页面上实现的效果分别如下图:

如果把@Html.EditorForModel()替换成下面的代码也可以实现同样的效果:

@foreach(var item in Model)
{
<tr>
<td>
<input type="checkbox" name="employeeIdsToDelete" id="employeeIdsToDelete" value="@item.ID">
</td>
<td>
@item.Name
</td>
<td>
@item.Name
</td>
<td>
@item.Name
</td>
</tr>
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: