我的数据库表中字段名从name1到name100,当然每条记录用到的字段数不一样,有的只有10项,有的只有20项,那么输出显示时就只显示有数据的字段内容,查询结果要按实际记录的情况而定,如下:
@model IEnumerable<mvc.Models.usertable>
@using System.Data;
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.name1)
</th>
<th>
@Html.DisplayNameFor(model => model.name2)
</th>
<th>
@Html.DisplayNameFor(model => model.name3)
</th>
<th>
@Html.DisplayNameFor(model => model.name4)
</th>
<th>
@Html.DisplayNameFor(model => model.name5)
</th>
<th>
@Html.DisplayNameFor(model => model.name6)
</th>
<th>
@Html.DisplayNameFor(model => model.name7)
</th>
<th>
@Html.DisplayNameFor(model => model.name8)
</th>
<th>
@Html.DisplayNameFor(model => model.name9)
</th>
<th>
@Html.DisplayNameFor(model => model.name10)
</th>
<th>
@Html.DisplayNameFor(model => model.name11)
</th>
<th>
@Html.DisplayNameFor(model => model.name12)
</th>
<th>
@Html.DisplayNameFor(model => model.name13)
</th>
<th>
@Html.DisplayNameFor(model => model.name14)
</th>
<th>
@Html.DisplayNameFor(model => model.name15)
</th>
<th>
@Html.DisplayNameFor(model => model.name16)
</th>
<th>
@Html.DisplayNameFor(model => model.name17)
</th>
<th>
@Html.DisplayNameFor(model => model.name18)
</th>
<th>
@Html.DisplayNameFor(model => model.name19)
</th>
<th>
@Html.DisplayNameFor(model => model.name20)
</th>
<th>
@Html.DisplayNameFor(model => model.name21)
</th>
<th>
@Html.DisplayNameFor(model => model.name22)
</th>
<th>
@Html.DisplayNameFor(model => model.name23)
</th>
<th>
@Html.DisplayNameFor(model => model.name24)
</th>
<th>
@Html.DisplayNameFor(model => model.name25)
</th>
……
上面是view文件中的语句逐条写,能否用循环语句,显示哪些字段由变量指定,比如我要显示的是其中一条记录的第1 个字段内容到第n个字段的内容,包括这和记录的姓名、性别、电话、住址等n项信息。关键问题在于这里的item.name10能否用变量指定,比如item.namex,即用变量x代表具体数字,请指导!
我的cs文件中内容如下
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvc.Models;
using System.Data.SqlClient;
using System.Reflection;
using System.Collections;
namespace mvc.Controllers
{
public class mvcController : Controller
{
private mvcconnection_dbEntities db = new mvcconnection_dbEntities();
// GET: /mvc/
public ActionResult Index()
{
return View(db.usertable.ToList());
}