ASP.NET MVC HTML列显示

3
我希望构建两列布局,第一列应包含FirstList中的项目,第二列应该包含SecondList中的项目。为什么在我的实现中它把一列放在另一列下面?如何修复这个问题?我需要使用CSS样式而不是HTML吗?
控制器和类:
namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        List<Employee> emp1 = new List<Employee>();
        emp1.Add(new Employee { Number="1",Text="dfsfdsfsafdsafdasfadsf"});
        emp1.Add(new Employee { Number = "2", Text = "asdfsa" });
        emp1.Add(new Employee { Number = "3", Text = "dfsfdsfdasfsafdsafdasfdsafsaadsf" });
        List<Employee> emp2 = new List<Employee>();
        emp2.Add(new Employee { Number = "1", Text = "dfsfdsfsafdsafdasfadsf" });
        emp2.Add(new Employee { Number = "2", Text = "asdfsa" });
        emp2.Add(new Employee { Number = "3", Text = "dfsfdsfdasfsafdsafdasfdsafsaadsf" });
        emp2.Add(new Employee { Number = "4", Text = "asdfsa" });
        emp2.Add(new Employee { Number = "5", Text = "dfsfdsfdasfsafdsafdasfdsafsaadsf" });
        return View(new IndexViewModel { FirstList = emp1, SecondList = emp2 });
    }
}
public class IndexViewModel
{
    public IEnumerable<Employee> FirstList { get; set; }

    public IEnumerable<Employee> SecondList { get; set; }
}
public class Employee
{
    public string Text { get; set; }

    public string Number { get; set; }
}
}

Index.cshtml

@model MvcApplication4.Controllers.IndexViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table>

<tbody>
    @foreach (var item in Model.FirstList)
    {
        <tr>
            <td>@item.Number</td>
            <td>@item.Text</td>
        </tr>
    }
    @foreach (var item in Model.SecondList)
    {
        <tr>
            <td>@item.Number</td>
            <td>@item.Text</td>
        </tr>
    }
</tbody>
</table>
2个回答

3

您正在将两个模型的值直接写入同一个表格中,这会导致它们互相覆盖。为了实现您想要的效果,您需要将这些值分别放在不同的表格中,并使用HTML和CSS进行适当的定位。

   @model MvcApplication4.Controllers.IndexViewModel
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table>

<tbody>
    @foreach (var item in Model.FirstList)
    {
        <tr>
            <td>@item.Number</td>
            <td>@item.Text</td>
        </tr>
    }
</tbody>
</table>
<table>
<tbody>
   @foreach (var item in Model.SecondList)
    {
        <tr>
            <td>@item.Number</td>
            <td>@item.Text</td>
        </tr>
    }
</tbody>
</table>

1
这段代码按照您的期望工作:

<table border="1">
    <thead>
        <th colspan="2">First List</th>
        <th colspan="2">Second List</th>
    </thead>
    <tbody>
        @for (int i = 0; i < Math.Max(Model.FirstList.Count(), Model.SecondList.Count()); i++)
        {
            Employee  first = Model.FirstList.Count() > i ? Model.FirstList.ToList()[i] : null;
            Employee second = Model.SecondList.Count() > i ? Model.SecondList.ToList()[i] : null;
            <tr>
                <td>@(first?.Number)</td>
                <td>@(first?.Text)</td>
                <td>@(second?.Number)</td>
                <td>@(second?.Text)</td>
            </tr>
        }
    </tbody>
</table>

你应该合并两个foreach循环,以便将列表并排显示。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接