2013-03-19 67 views
1

我通过控制器将一组控件(复选框,单选按钮等)作为列表传递给其相应的视图。这是我想要实现的。根据MVC中的条件动态创建表中的行

  1. 表中的每一行都应该有3列。
  2. 如果对象数大于3,则应在表中创建一个新行。

我使用MVC 4

+2

除了发布您的需求之外,您忘记了展示了迄今为止尝试的内容以及您遇到的代码遇到的困难。现在你听起来更像是餐厅里的客户,而不是软件开发人员在StackOverflow上询问特定的编程相关问题。 – 2013-03-19 06:49:11

回答

2

这是哟能做到这一点MVC中的方式这样做:

型号:

public class Class1 
    { 
      public string numbers { get; set; } 
    } 

控制器代码:

public ActionResult About() 
     { 
      ViewBag.Message = "Your app description page."; 
      //Sample1--load array data using linq 
      List<Class1> model = new List<Class1>(); 
      int[] numbersdata = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0, 15, 14, 11, 13, 19, 18, 16, 17, 12, 10 }; 

      var lowNums = from n in numbersdata where n > 5 select n; 

      foreach (var x in lowNums) 
      { 
       model.Add(new Class1() 
       { 
        numbers = x.ToString() 
       }); 
      } 
      return View(model); 
     } 

查看:

@model IEnumerable<MvcApplication1.Models.Class1> 
@using (Html.BeginForm()) 
{ 
    <table width="960px"> 
     <tr> 
      @{ 
    int crow = 1; 
    foreach (var item in Model) 
    { 
       <td style="border: 1px solid black;" width="600px"> 
        <ul style="list-style: none;"> 
         <li> 
          @Html.TextBox("txt") 
         </li> 

        </ul> 
       </td> 
     if (crow % 3 == 0) 
     {              
       <tr> 
        <td style="width: 285px; height: 50px"> 
        </td> 
       </tr> 
     } 
     crow++; 
     } 
    } 
     </tr> 
    </table> 
}