@Html.EditorForModel()用法

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













  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值