c# - ASP.NET mvc html 列显示

标签 c# html css asp.net asp.net-mvc-4

我想构建两列,第一列应包含 FirstList 的元素,第二列应包含 SecondList 的元素。为什么在我的实现中将一列放在另一列下面?如何解决这个问题?我需要使用 css 样式而不是 html?

Controller 和类:

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; }
}
}

索引.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>

最佳答案

您将两个模型的值直接叠加写入同一个表中。为了实现我认为您正在尝试做的事情,您需要将这些值分离到单独的表中,并使用 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>

关于c# - ASP.NET mvc html 列显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36607630/

相关文章:

c# - 任务<T>如何返回

javascript - HTML 样式表更改错误

javascript - Css悬停动画闪烁/循环

html - 修复了容器 div 中的文本滚动

c# - 滥用 Dispose() 来处理不被释放的对象?

c# - DropDownList 事件不会触发

c# - 如何索引和搜索 Lucene.NET 中的日期时间字段?

html - 滚动顶部导航栏

javascript - Paper.JS 框架 "Selection Outlines"

javascript - D3 折线图不清晰