C# 列出模型剃刀视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16665863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:37:28  来源:igfitidea点击:

List model razor view

c#asp.netasp.net-mvcrazor

提问by Lamloumi Afif

I have an application asp.net mvc . in the controller i have this:

我有一个应用程序 asp.net mvc 。在控制器中我有这个:

 public ActionResult Index()
        {
            Upload.Models.ClientModels model1 = new Models.ClientModels();
            ArrayList client = model1.Client_List();

            Upload.Models.AkeoModels model2 = new Models.AkeoModels();
            ArrayList akeo = model2.Akeo_List();


            ArrayList model = new ArrayList();
            model.Add(client);
            model.Add(akeo);
            return View(model);




        }

I passed two concatenated list to the view Index as a model:

我将两个串联列表作为模型传递给视图索引:

@{
    ViewBag.Title = "Bienvenue";
    int i = 0;
}

<hgroup class="title">
    <h1 style="color:darkcyan">Liste des clients</h1>
</hgroup>

<section >

<form>


<table style="margin-top: 50px;">
    <tr ><td ></td>
        <td ><center><b>Login</b></center></td>
        <td><center><b>Email</b></center></td>
        <td><center><b>Password</b></center></td>
        <td><center><b>Name</b></center></td>

    </tr>
    @{
        Upload.Models.ClientModels client = null;
        int j = 0;
        while( j != -1)
    {
        try
        {
            client = Model[j];
          <tr>
        <td width=10px>
            @if (i == 0)
            {
      <input type="radio" 
        name="color" checked >
            }
            else
            {<input type="radio"  name="color" >
            }      
    </td>
    <td>
      <a type="text" 
        name="color" >@client.Login</a>
    </td>

    <td>
      <a type="text" 
        name="color" >@client.Mail</a>
    </td>
      <td>
      <a type="text" 
        name="color" >@client.Password</a>
    </td>
      <td>
      <a type="text" 
        name="color" >@client.Name</a>
    </td>
  </tr>
            i++;
            j++;
        }
        catch {j = -1;}
    }
        i = 0;
    }

</table>

</form>

</section>

<section style="margin-top: 30px">

    <a type="button"   style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;margin-left:150px"  href="@Url.Action("Delete_client", "Admin")">Supprimer</a>
    <a type="button" style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;" href="@Url.Action("Edit_client", "Admin",new { Id = 1 })">Editer</a>
    <br /> <br />
     <a href="@Url.Action("Create","Client")" style="color:blue; margin-top : 30px;margin-left:150px">Créer un nouveau compte</a>

    </section>
    <br />
<section>
<hgroup class="title">
    <h1 style="color:darkcyan">Liste des akeos</h1>
</hgroup>

<section >

<form>


<table style="margin-top: 50px;">
    <tr ><td ></td>
        <td ><center><b>Login</b></center></td>
        <td><center><b>Email</b></center></td>
        <td><center><b>Password</b></center></td>
        <td><center><b>Name</b></center></td>

    </tr>
     @{
        Upload.Models.AkeoModels akeo = null;
        int k = 0;
        while( k < Model.Count)
    {
        try
        {
            akeo = Model[k];

  <tr>
        <td width=10px>
            @if (i == 0){
      <input type="radio" 
        name="color" checked >
            }
            else{<input type="radio"  name="color" >
            }      
    </td>
    <td>
      <a type="text" 
        name="color" >@akeo.Login</a>
    </td>

    <td>
      <a type="text" 
        name="color" >@akeo.Mail</a>
    </td>
      <td>
      <a type="text" 
        name="color" >@akeo.Password</a>
    </td>
      <td>
      <a type="text" 
        name="color" >@akeo.Name</a>
    </td>
  </tr>
         i++;
            k++;
        }
        catch {k++;}
    }
    }
</table>
</form>

</section>






 <section style="margin-top: 30px">


    <a type="button"   style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;margin-left:150px"  href="@Url.Action("Delete_akeo", "Admin",new { Id = 1})">Supprimer</a>
    <a type="button" style="width:120px;display:inline-block; height:20px;text-decoration:none;color:white;text-align:center;background-color:darkcyan;padding:5px;border-style:outset;border-width:2px;border-color:darkcyan;" href="@Url.Action("Edit_akeo", "Admin",new { Id = 1 })">Editer</a>
    <br /> <br /> <br />
     <a href="@Url.Action("Create","Akeo")" style="color:blue; margin-top : 30px;margin-left:150px">Créer un nouveau compte</a>

    </section>

    </section>

The problem is : the values of the list is not shown . in the view Index only the labels and the buttons below the tables are shown but the content of the model isn't. i'am debugging the program and the model is not empty, it contains two elements.

问题是:列表的值没有显示。在视图索引中,仅显示标签和表格下方的按钮,但不显示模型的内容。我正在调试程序,模型不是空的,它包含两个元素。

  1. Where is the error in the view?
  2. How can i fix it?
  1. 视图中的错误在哪里?
  2. 我该如何解决?

采纳答案by Darren

You should not use ArrayList. You should create a custom type that contains properties from both models.

你不应该使用ArrayList. 您应该创建一个包含来自两个模型的属性的自定义类型。

For example:

例如:

public class CustomModel 
{
   public int PropertyOne { get; set; }
   public string PropertyTwo { get; set; }
}

Or alternatively you can have:

或者,您可以拥有:

public class CustomModel2 
{
   public List<Models.ClientModels> ClientModels { get; set; }
   public List<Models.Akeo_List> AkeoModels { get; set; }
}

Controller:

控制器:

public ActionResult Index() 
{
  var model = new List<CustomModel>();
  model.Add(new CustomModel() { });
  return View(model);
}

View

看法

@Model List<CustomModel>

foreach(var cm in @Model) {
     cm.PropertyOne
}

If you were using CustomModel2you could iterate over all of the clients by doing:

如果您正在使用,CustomModel2您可以通过执行以下操作来遍历所有客户端:

   foreach (var client in @Model.ClientModels) {
       @client.ClientName
   }

回答by Nicholas King

You are doing the M and V bit of the MVC a little strange have you tried using @Html.EditorFor()? as for what you are experiencing with your code.

你正在做 MVC 的 M 和 V 位有点奇怪你试过使用 @Html.EditorFor() 吗?至于您在使用代码时遇到了什么。

You have not included a

你还没有包括

@model decleration at the top of the view.

视图顶部的@model 声明。

EDIT: Sorry I have just re read your code and this bit is incorrect.

编辑:抱歉,我刚刚重新阅读了您的代码,但这一点不正确。



you set client to null then loop through it.

您将 client 设置为 null 然后循环遍历它。

Upload.Models.ClientModels client = null;

Upload.Models.ClientModels 客户端 = null;



id recommend reading some of Darins post https://stackoverflow.com/users/29407/darin-dimitrovhe has excellent MVC posts about model and view construction

id 建议阅读一些 Darins 帖子https://stackoverflow.com/users/29407/darin-dimitrov他有关于模型和视图构建的优秀 MVC 帖子