开发者社区> 问答> 正文

你好,我遇到的问题是:网站建设中的数据库操作问题,我用ASP.NET MVC建站的,请予帮助,谢谢

 我的数据库表中字段名从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()); 
        }

展开
收起
1321143746133045 2019-01-15 10:38:42 659 0
1 条回答
写回答
取消 提交回答
  • 旺旺:nectar2。
    您好,具体是什么样的问题呢?是网站程序连接不上数据库吗?
    2019-01-15 11:00:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载