如何使用asp.net mvc的EditorTemplate [英] How to use asp.net mvc EditorTemplate

查看:111
本文介绍了如何使用asp.net mvc的EditorTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了EditorTemplates自动加载,但是从asp.net MVC 2现在3剃须刀,我仍然无法得到这个工作。

我的模型是这样的:

 公共类RoleViewModel
{
    公众诠释角色ID {搞定;组; }
    公共BOOL InRole {搞定;组; }
    公共字符串角色名{获得;组; }
}公共类UserViewModel
{
    公众用户用户{搞定;组; }
    公共IEnumerable的< RoleViewModel>角色{搞定;组; }
}

我的看法是这样的:

〜/查看/角色/ Edit.cshtml

  @model Project.Web.ViewModel.UserViewModel
@using(Html.BeginForm()){
   @ Html.EditorFor(型号=> model.Roles)
   <! - 在这里其他的东西 - >
}

〜/查看/角色/ EditorTemplates / RoleViewModel.cshtml

  @model Project.Web.ViewModel.RoleViewModel
@foreach(VAR我在模型)
{
    < D​​IV>
    @ i.RoleName
    @ Html.HiddenFor(型号=> i.RoleId)
    @ Html.CheckBoxFor(型号=> i.InRole)
    < / DIV>
}

如果我移动从 EditorTemplate 来实际页面的内容,那么它的工作原理,它显示的复选框等,但这个目前的设置,所有的显示出来是角色的数量的计数。

我在做什么错了?


解决方案

〜/查看/角色/ EditorTemplates / RoleViewModel.cshtml

  @model MvcApplication16.Controllers.RoleViewModel
< D​​IV>
    @ Model.RoleName
    @ Html.HiddenFor(M = GT; m.RoleId)
    @ Html.CheckBoxFor(M = GT; m.InRole)
< / DIV>

〜/查看/角色/ Edit.cshtml

  @model MvcApplication16.Controllers.UserViewModel
@using(Html.BeginForm()){
   @ Html.EditorFor(M = GT; m.Roles)
   <! - 在这里其他的东西 - >
}

模式

 公共类UserViewModel {
    公众用户用户{搞定;组; }
    公共IEnumerable的< RoleViewModel>角色{搞定;组; }
}公共类RoleViewModel {
    公众诠释角色ID {搞定;组; }
    公共BOOL InRole {搞定;组; }
    公共字符串角色名{获得;组; }
}公共类用户{
    公共字符串名称{;组; }
}

控制器

 公众的ActionResult编辑(){
    返回查看(
        新UserViewModel(){
            用户=新用户(){名称=测试},
            角色=新的List< RoleViewModel>(){
                新RoleViewModel(){
                    角色ID = 1,
                    InRole = TRUE,
                    ROLENAME =测试角色}}
        });
}

以上code的作品就好。与你比较一下,看看是否你看到什么不对劲:)

I read that EditorTemplates are loaded automatically, but from asp.net mvc 2 and now 3 with razor, I still cant get this to work.

My model looks like this:

public class RoleViewModel
{
    public int RoleId { get; set; }
    public bool InRole { get; set; }
    public string RoleName { get; set; }
}

public class UserViewModel
{
    public User User { get; set; }
    public IEnumerable<RoleViewModel> Roles { get; set; }
}

My view looks like this:

~/Views/Roles/Edit.cshtml

@model Project.Web.ViewModel.UserViewModel
@using (Html.BeginForm()) {
   @Html.EditorFor(model => model.Roles)
   <!-- Other stuff here -->
}

~/Views/Roles/EditorTemplates/RoleViewModel.cshtml

@model Project.Web.ViewModel.RoleViewModel
@foreach (var i in Model)
{
    <div>
    @i.RoleName
    @Html.HiddenFor(model => i.RoleId)
    @Html.CheckBoxFor(model => i.InRole)
    </div>
}

If i move the content from the EditorTemplate to the actual page, then it works, it shows the checkbox, etc. But with this current setup, all that shows up is the count of the number of roles.

What am I doing wrong?

解决方案

~/Views/Roles/EditorTemplates/RoleViewModel.cshtml

@model MvcApplication16.Controllers.RoleViewModel
<div>
    @Model.RoleName
    @Html.HiddenFor(m => m.RoleId)
    @Html.CheckBoxFor(m => m.InRole)
</div>

~/Views/Roles/Edit.cshtml

@model MvcApplication16.Controllers.UserViewModel
@using (Html.BeginForm()) {
   @Html.EditorFor(m => m.Roles)
   <!-- Other stuff here -->
}

Models

public class UserViewModel {
    public User User { get; set; }
    public IEnumerable<RoleViewModel> Roles { get; set; }
}

public class RoleViewModel {
    public int RoleId { get; set; }
    public bool InRole { get; set; }
    public string RoleName { get; set; }
}

public class User {
    public string Name { get; set; }
}

Controller

public ActionResult Edit() {
    return View(
        new UserViewModel() {
            User = new User() { Name = "Test" },
            Roles = new List<RoleViewModel>() { 
                new RoleViewModel() { 
                    RoleId = 1, 
                    InRole = true, 
                    RoleName = "Test Role" }}
        });
}

The above code works just fine. Compare it with yours and see if you see anything amiss :)

这篇关于如何使用asp.net mvc的EditorTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆