Asp.net mvc 5 Net MVC:如何使用editorfor和编辑器模板生成html表
现在我仍然像下面的代码示例一样生成html表,但我想知道如何使用编辑器生成html表 查看我的完整razor和view模型代码,并指导我如何实现目标Asp.net mvc 5 Net MVC:如何使用editorfor和编辑器模板生成html表,asp.net-mvc-5,Asp.net Mvc 5,现在我仍然像下面的代码示例一样生成html表,但我想知道如何使用编辑器生成html表 查看我的完整razor和view模型代码,并指导我如何实现目标 @model MVCCRUDPageList.Models.StudentListViewModel @{ ViewBag.Title = "Index"; } <h2>CREATE TABULAR UI WITH HTML TABLE</h2> @using (Html.BeginForm("Index", "
@model MVCCRUDPageList.Models.StudentListViewModel
@{
ViewBag.Title = "Index";
}
<h2>CREATE TABULAR UI WITH HTML TABLE</h2>
@using (Html.BeginForm("Index", "HtmlTable", FormMethod.Post))
{
<div class="form-group">
<div class="col-md-12 table-responsive">
<table class="table table-bordered table-hover">
<tr>
<th>
Row No
</th>
<th>
ID
</th>
<th>
Name
</th>
<th>
Country
</th>
<th>
Hobbies
</th>
<th>
Sex
</th>
</tr>
}
@for (int x=0; x<=Model.Students.Count-1;x++)
{
<tr>
<td>
<label>@(x+1)</label>
</td>
<td>
@Html.TextBoxFor(m => m.Students[x].ID)
</td>
<td>
@Html.TextBoxFor(m => m.Students[x].Name)
</td>
<td>
@Html.DropDownListFor(m => m.Students[x].CountryID,
new SelectList(Model.Country, "ID", "Name", Model.Students[x].CountryID),
"-- Select Countries--", new { id = "cboCountry", @class = "edit-mode" })
</td>
<td>
@for (var i = 0; i < Model.Students.FirstOrDefault().Hobbies.Count; i++)
{
<div class="checkbox">
@Html.HiddenFor(m => m.Students[x].Hobbies[i].ID)
@Html.HiddenFor(m => m.Students[x].Hobbies[i].Name)
@Html.CheckBoxFor(m => m.Students[x].Hobbies[i].Checked)
@Html.LabelFor(m => m.Students[x].Hobbies[i].Name, Model.Students[x].Hobbies[i].Name)
</div>
}
</td>
<td>
@for (var i = 0; i < Model.Sex.Count; i++)
{
<div class="checkbox">
@Html.HiddenFor(m => Model.Sex[i].ID)
@Html.HiddenFor(m => Model.Sex[i].SexName)
@Html.RadioButtonFor(m => m.Students[x].SexID, Model.Sex[i].ID)
@Html.LabelFor(m => m.Students[x].SexID, Model.Sex[i].SexName)
</div>
}
</td>
</tr>
}
</table>
</div>
<input type="submit" value="Submit" />
</div>
}
@model MVCCRUDPageList.Models.StudentListViewModel
@{
ViewBag.Title=“Index”;
}
使用HTML表格创建表格用户界面
@使用(Html.BeginForm(“Index”,“HtmlTable”,FormMethod.Post))
{
排号
身份证件
名称
国
业余爱好
性别
}
@对于(int x=0;x m.Students[x].ID)
@Html.TextBoxFor(m=>m.Students[x].Name)
@Html.DropDownListFor(m=>m.Students[x].CountryID,
新的选择列表(Model.Country,“ID”,“Name”,Model.Students[x].CountryID),
“--选择国家——”,新的{id=“cboCountry”,@class=“编辑模式”})
@for(var i=0;im.Students[x].爱好[i].ID)
@Html.HiddenFor(m=>m.Students[x]。爱好[i]。姓名)
@Html.CheckBoxFor(m=>m.Students[x]。爱好[i]。选中)
@Html.LabelFor(m=>m.Students[x]。爱好[i]。名称,模型。学生[x]。爱好[i]。名称)
}
@对于(变量i=0;iModel.Sex[i].ID)
@Html.HiddenFor(m=>Model.Sex[i].SexName)
@(m=>m.Students[x].SexID,Model.Sex[i].ID)
@Html.LabelFor(m=>m.Students[x].SexID,Model.Sex[i].SexName)
}
}
}
视图模型代码
公共类学生列表视图模型
{
公共IList学生{get;set;}
公共列表国家{get;set;}
公共列表性{get;set;}
公共学生列表视图模型()
{
学生=新名单
{
新生
{
ID=1,Name=“Keith”,CountryID=0,SexID=“F”,
爱好=新列表
{
新爱好{ID=1,Name=“Football”,Checked=false},
新爱好{ID=2,Name=“Hocky”,Checked=false},
新爱好{ID=3,Name=“Cricket”,Checked=false}
}
},
新生
{
ID=2,Name=“Paul”,CountryID=2,
爱好=新列表
{
新爱好{ID=1,Name=“Football”,Checked=false},
新爱好{ID=2,Name=“Hocky”,Checked=false},
新爱好{ID=3,Name=“Cricket”,Checked=false}
}
},
新生
{
ID=3,Name=“Sam”,CountryID=3,
爱好=新列表
{
新爱好{ID=1,Name=“Football”,Checked=false},
新爱好{ID=2,Name=“Hocky”,Checked=false},
新爱好{ID=3,Name=“Cricket”,Checked=false}
}
}
};
国家=新名单
{
新国家{ID=1,Name=“India”},
新国家{ID=2,Name=“UK”},
新国家{ID=3,Name=“USA”}
};
性别=新名单
{
新性别{ID=“M”,SexName=“Male”},
新性别{ID=“F”,SexName=“女性”}
};
}
}
请指导我如何重新构造我的razor代码,以使用editorfor和编辑器模板获得干净的代码。编辑器模板的名称是什么
感谢使用
@Html.EditorFor(m=>m.SomeProperty)
使用EditorTemplate
(其中SomeProperty
是复杂对象或复杂对象的集合),那么模板必须位于/Views/Shared/EditorTemplates
或/Views/YourControllerName/EditorTemplates
文件夹中,并与SomeProperty
的类名相同
在您的情况下,如果您希望为学生
的类型使用编辑器或模板
,则模板(部分视图)将命名为Student.cshtml
@model yourAssembly.Student
<tr>
<td>@Html.TextBoxFor(m => m.ID)</td>
<td>@Html.TextBoxFor(m => m.Name)<td>
....
<tr>
其中StudentListViewModel
中的Countries
属性应为
public IEnumerable<SelectListItem> Countries { get; set; }
注意LabelFor()
应该用于Checked
属性,而不是当前使用的Name
属性
然后在Student.cshtml
模板中,使用
<td>@Html.EditorFor(m => m.Hobbies)<td>
@Html.EditorFor(m=>m.cabiods)
要为每个
爱好生成正确的html
为什么要通过like@html.EditorFor(m=>m.Students)
m.学生代表单个学生,但我们需要通过like@html.EditorFor(m=>m.Students[i])
我是对的还是错的?请看这一行@html.EditorFor(m=>m.Stu
@Html.EditorFor(m => m.Students, new { Countries = Model.Countries})
public IEnumerable<SelectListItem> Countries { get; set; }
<td>@Html.DropDownListFor(m => m.CountryID, (IEnumerable<SelectListItem>)ViewData["Countries "], "-- Select Country--", new { ... })<td>
@model yourAssembly.Hobby
<div class="checkbox">
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.Name)
@Html.CheckBoxFor(m => m.Checked)
@Html.LabelFor(m => m.Checked, Model.Name)
</div>
<td>@Html.EditorFor(m => m.Hobbies)<td>