C#MVC列表并在同一视图中创建

C#MVC列表并在同一视图中创建,c#,asp.net-mvc,asp.net-mvc-viewmodel,C#,Asp.net Mvc,Asp.net Mvc Viewmodel,我有一个这样的模型 public class Roles { [Key] public string RoleId {get;set;} public string RoleName {get;set;} } 我在这方面面临的挑战是为列表创建一个视图,然后创建。我所做的每一次尝试都以数据类型错误告终。我该怎么办?回答1: 要隐藏@Html.EditorForModel()中的某些字段(如注释部分所述),必须使用: [ScaffoldColumn(false)]属性

我有一个这样的模型

public class Roles
{
     [Key]
     public string RoleId {get;set;}
     public string RoleName {get;set;}
}

我在这方面面临的挑战是为列表创建一个视图,然后创建。我所做的每一次尝试都以数据类型错误告终。我该怎么办?

回答1:

要隐藏
@Html.EditorForModel()
中的某些字段(如注释部分所述),必须使用:

[ScaffoldColumn(false)]
属性

例:-

答案2:

以及在同一视图上创建和显示列表:

型号:

public class Roles
{
 [Key]
 public string RoleId {get;set;}
 public string RoleName {get;set;}
 public List<Roles> Roleslist { get;set; }  //Take a list in Model as shown
}
公共类角色
{
[关键]
公共字符串RoleId{get;set;}
公共字符串RoleName{get;set;}
public List Roleslist{get;set;}//在模型中获取一个列表,如图所示
}
视图:

 <div id="mainwrapper">

 @using (Html.BeginForm("// Action Name //", "// Controller Name //", FormMethod.Post, new { @id = "form1" }))
  {  

   @Html.TextBoxFor(m => m.RoleName)
   <input type="submit" value="Save"/>

  }
  <div id="RolesList">
   @if(Model!=null)
    {
      if(Model.Roleslist!=null)
      {
       foreach(var item in Model.Roleslist) //Just Populate Roleslist with values on controller side
       {
       //here item will have your values of RoleId and RoleName 
       }
      }
    }
  </div>

  </div>

@使用(Html.BeginForm(“//Action Name//”、“//Controller Name//”、FormMethod.Post、new{@id=“form1”}))
{  
@Html.TextBoxFor(m=>m.RoleName)
}
@如果(型号!=null)
{
if(Model.Roleslist!=null)
{
foreach(Model.Roleslist中的var项)//只需在控制器端用值填充Roleslist
{
//此处项目将包含您的RoleId和RoleName值
}
}
}

答案1:

要隐藏
@Html.EditorForModel()
中的某些字段(如注释部分所述),必须使用:

[ScaffoldColumn(false)]
属性

例:-

答案2:

以及在同一视图上创建和显示列表:

型号:

public class Roles
{
 [Key]
 public string RoleId {get;set;}
 public string RoleName {get;set;}
 public List<Roles> Roleslist { get;set; }  //Take a list in Model as shown
}
公共类角色
{
[关键]
公共字符串RoleId{get;set;}
公共字符串RoleName{get;set;}
public List Roleslist{get;set;}//在模型中获取一个列表,如图所示
}
视图:

 <div id="mainwrapper">

 @using (Html.BeginForm("// Action Name //", "// Controller Name //", FormMethod.Post, new { @id = "form1" }))
  {  

   @Html.TextBoxFor(m => m.RoleName)
   <input type="submit" value="Save"/>

  }
  <div id="RolesList">
   @if(Model!=null)
    {
      if(Model.Roleslist!=null)
      {
       foreach(var item in Model.Roleslist) //Just Populate Roleslist with values on controller side
       {
       //here item will have your values of RoleId and RoleName 
       }
      }
    }
  </div>

  </div>

@使用(Html.BeginForm(“//Action Name//”、“//Controller Name//”、FormMethod.Post、new{@id=“form1”}))
{  
@Html.TextBoxFor(m=>m.RoleName)
}
@如果(型号!=null)
{
if(Model.Roleslist!=null)
{
foreach(Model.Roleslist中的var项)//只需在控制器端用值填充Roleslist
{
//此处项目将包含您的RoleId和RoleName值
}
}
}

同时检查控制器,确保未将
Roles.ToList()
传递到
CreateRole
视图。 您可以这样做:

  [HttpGet]
    public ActionResult CreateRole()
    {
        var roles = from ur in db.roles
                    orderby ur.RoleName
                    select ur;
        ViewBag.Listroles = roles.ToList();
        return View();
    }
其中
db
是您的
DbContext

你的观点应该是这样的:

  @{

      if(ViewBag.Listroles != null){
      foreach (var roles in ViewBag.Listroles)
        {
            <tr>

                <td>@roles.RoleName</td>
                <td>
                      @Html.ActionLink("Edit", "EditRoles", new { id=roles.RoleId }) |
                      @Html.ActionLink("Details", "Details", new { id=roles.RoleId }) |
                      @Html.ActionLink("Delete", "Delete", new { id=roles.RoleId })
                 </td>
            </tr>
        }

      }

    }
@{
if(ViewBag.Listroles!=null){
foreach(ViewBag.Listroles中的变量角色)
{
@RoleName
@ActionLink(“编辑”、“编辑角色”,新的{id=roles.RoleId})|
@ActionLink(“详细信息”,“详细信息”,新的{id=roles.RoleId})|
@ActionLink(“删除”,“删除”,新的{id=roles.RoleId})
}
}
}

如果我的回答有帮助,请告诉我。

同时检查控制器,确保没有将
角色.ToList()
传递到
CreateRole
视图。 您可以这样做:

  [HttpGet]
    public ActionResult CreateRole()
    {
        var roles = from ur in db.roles
                    orderby ur.RoleName
                    select ur;
        ViewBag.Listroles = roles.ToList();
        return View();
    }
其中
db
是您的
DbContext

你的观点应该是这样的:

  @{

      if(ViewBag.Listroles != null){
      foreach (var roles in ViewBag.Listroles)
        {
            <tr>

                <td>@roles.RoleName</td>
                <td>
                      @Html.ActionLink("Edit", "EditRoles", new { id=roles.RoleId }) |
                      @Html.ActionLink("Details", "Details", new { id=roles.RoleId }) |
                      @Html.ActionLink("Delete", "Delete", new { id=roles.RoleId })
                 </td>
            </tr>
        }

      }

    }
@{
if(ViewBag.Listroles!=null){
foreach(ViewBag.Listroles中的变量角色)
{
@RoleName
@ActionLink(“编辑”、“编辑角色”,新的{id=roles.RoleId})|
@ActionLink(“详细信息”,“详细信息”,新的{id=roles.RoleId})|
@ActionLink(“删除”,“删除”,新的{id=roles.RoleId})
}
}
}


如果我的回答有帮助,请告诉我。

为什么要为简单视图制作viewmodels,仅在某些复杂操作中使用viewmodels…只需为您的viewLol制作一个模型。所有这些我都是在困惑的状态下做的,但是当我使用“@Html.EditorForModel()”时,我如何防止
RoleId
显示在我的视图中呢?@Peter..我想我的答案足够详细,可以让你理解..?如果你需要更多的帮助,那么就评论一下..你为什么要为简单的视图制作视图模型,仅对某些复杂操作使用viewmodels…只需为您的viewLol创建一个模型。所有这些我都是在困惑的状态下做的,但是当我使用“@Html.EditorForModel()”时,我如何防止
RoleId
显示在我的视图中呢?@Peter..我想我的答案足够详细,可以让你理解..???如果你需要更多的帮助,那么评论一下…你的答案很好!谢谢。欢迎您…@PeterError System.NullReferenceException:Object reference未设置为
foreach(Model.Roleslist中的var项)
上的对象实例,我想我可以处理它。你怎么看?是的,这个错误会出现。角色列表中应该有一些东西要显示。只需查看我的更新答案…只需再给它一个包装,如上图所示。你的好!谢谢。欢迎您…@PeterError System.NullReferenceException:Object reference未设置为
foreach(Model.Roleslist中的var项)
上的对象实例,我想我可以处理它。你认为呢?是的,这个错误会出现。模型中应该有一些东西。角色列表要显示。只需查看我的更新答案…只需再做一次如上所示的总结。谢谢Peter。我很高兴这有帮助。谢谢彼得。我很高兴这有帮助。