c# - List model razor view -
i have application asp.net mvc . in controller have this:
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 2 concatenated list view index model:
@{ viewbag.title = "bienvenue"; int = 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;} } = 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 : values of list not shown . in view index labels , buttons below tables shown content of model isn't. i'am debugging program , model not empty, contains 2 elements.
- where error in view?
- how can fix it?
you should not use arraylist
. should create custom type contains properties both models.
for example:
public class custommodel { public int propertyone { get; set; } public string propertytwo { get; set; } }
or alternatively 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 using custommodel2
iterate on of clients doing:
foreach (var client in @model.clientmodels) { @client.clientname }
Comments
Post a Comment