none
MVC5 二维数据与一维数据进行关联,通过一维数组可key,检索二维数据的value RRS feed

  • 问题

  • MVC5项目二维数组问题

    1. MoKuai.LaoHuangLi文件夹下有一个类Laohuangli_Models.cs,用力model,代码如下:

    namespace suwenhua.MoKuai.LaoHuangLi

    {

        //{2018,11,22,狗,戊戌,平地木,太岁姜武,九紫,十月,小,癸亥,大海水,五黄,坎卦,女宿,十五,戊午,天上火,七赤,危,角宿,冬季,立冬,戌时,11,7,19,32,冬季,小雪,酉时,11,22,17,01}

        public class Laohuangli_Models

        {

            public string YangLiNian { get; set; }

            public string YangLiYue { get; set; }

            public string YangLiRi { get; set; }

            public string NongLiShuXiang { get; set; }

            public string NongLiNianJiaZi { get; set; }

            public string NongLiNianJiaZiWuXing { get; set; }

            public string NongLiNianTaiSui { get; set; }

            public string NongLiNianJiuXin { get; set; }

            public string NongLiYueFen { get; set; }

            public string NongLiYueDaXiao { get; set; }

            public string NongLiYueJiaZi { get; set; }

            public string NongLiYueJiaZiWuXing { get; set; }

            public string NongLiYueJiuXin { get; set; }

            public string NongLiYueBaGua { get; set; }

            public string NongLiYueErShiBaXiu { get; set; }

            public string NongLiRi { get; set; }

            public string NongLiRiJiaZi { get; set; }

            public string NongLiRiJiaZiWuXing { get; set; }

            public string NongLiRiJiuXin { get; set; }

            public string NongLiRiJianChuShiErZhi { get; set; }

            public string NongLiRiErShiBaXiu { get; set; }

            public string NongLiErShiSiJieQi_JiJie { get; set; }

            public string NongLiErShiSiJieQi_JieQi { get; set; }

            public string NongLiErShiSiJieQi_ShiChen { get; set; }

            public string NongLiErShiSiJieQi_YangLiYue { get; set; }

            public string NongLiErShiSiJieQi_YangLiRi { get; set; }

            public string NongLiErShiSiJieQi_Shi { get; set; }

            public string NongLiErShiSiJieQi_Fen { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_JiJie { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_JieQi { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_ShiChen { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_YangLiYue { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_YangLiRi { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_Shi { get; set; }

            public string NongLiErShiSiJieQi_ZhongQi_Fen { get; set; }

            

        }

    }

    2. 同在MoKuai.LaoHuangLi文件夹下有另一个类Laohuangli_Models_Dangqianyue.cs,用力model,其引用Laohuangli_Models,目的是复用KEY,它是由Laohuangli_Models(一维数据)组合成的二维数据,代码如下:

    using System.Collections.Generic;

    using System.Collections;

    namespace suwenhua.MoKuai.LaoHuangLi

    {

        public class Laohuangli_Models_Dangqianyue    {

            public List<Laohuangli_Models>  Laohuangli_Models_Dangqianyue_ { get; set; }

    }

    3. 现在suwenhua.Controllers文件夹下,有一个与view对应的Dangqianyue(),通过以已知二维数组值laohuangli_dangqianyueLaohuangli_ModelsLaohuangli_Models_Dangqianyue进行赋值,使其两者进行关联,便于在view中通过Laohuangli_Modelskey进行检索Laohuangli_Models_Dangqianyue检索value,具体代码如下:

    namespace suwenhua.Controllers

    {

        public class HomeController : Controller

    {

                 public ActionResult Dangqianyue()

            {

                ViewBag.Title = "当前月";

                //声明 Laohuangli

                Laohuangli laohuangli = new Laohuangli();

                //引用Laohuangli类型下Laohuangli_Dangqianyue()方法,获取二维数组

                string[,] laohuangli_dangqianyue = laohuangli.Laohuangli_Dangqianyue();

                //引用Laohuangli_Models_Dangqianyue,进行声明

                Laohuangli_Models_Dangqianyue laohuangli_models_dangqianyue = new Laohuangli_Models_Dangqianyue();

                laohuangli_models_dangqianyue.Laohuangli_Models_Dangqianyue_ = new List<Laohuangli_Models>();

                for (int row = 0; row <= laohuangli_dangqianyue.GetLength(0); row++)

                {

                    laohuangli_models_dangqianyue.Laohuangli_Models_Dangqianyue_.Add(

                       new Laohuangli_Models()

                       {

                        YangLiNian = laohuangli_dangqianyue[row, 0],

                        YangLiYue = laohuangli_dangqianyue[row, 1],

                        YangLiRi = laohuangli_dangqianyue[row, 2],

                        NongLiShuXiang = laohuangli_dangqianyue[row, 3],

                        NongLiNianJiaZi = laohuangli_dangqianyue[row, 4],

                        NongLiNianJiaZiWuXing = laohuangli_dangqianyue[row, 5],

                        NongLiNianTaiSui = laohuangli_dangqianyue[row, 6],

                        NongLiNianJiuXin = laohuangli_dangqianyue[row, 7],

                        NongLiYueFen = laohuangli_dangqianyue[row, 8],

                        NongLiYueDaXiao = laohuangli_dangqianyue[row, 9],

                        NongLiYueJiaZi = laohuangli_dangqianyue[row, 10],

                        NongLiYueJiaZiWuXing = laohuangli_dangqianyue[row, 11],

                        NongLiYueJiuXin = laohuangli_dangqianyue[row, 12],

                        NongLiYueBaGua = laohuangli_dangqianyue[row, 13],

                        NongLiYueErShiBaXiu = laohuangli_dangqianyue[row, 14],

                        NongLiRi = laohuangli_dangqianyue[row, 15],

                        NongLiRiJiaZi = laohuangli_dangqianyue[row, 16],

                        NongLiRiJiaZiWuXing = laohuangli_dangqianyue[row, 17],

                        NongLiRiJiuXin = laohuangli_dangqianyue[row, 18],

                        NongLiRiJianChuShiErZhi = laohuangli_dangqianyue[row, 19],

                        NongLiRiErShiBaXiu = laohuangli_dangqianyue[row, 20],

                        NongLiErShiSiJieQi_JiJie = laohuangli_dangqianyue[row, 21],

                        NongLiErShiSiJieQi_JieQi = laohuangli_dangqianyue[row, 22],

                        NongLiErShiSiJieQi_ShiChen = laohuangli_dangqianyue[row, 23],

                        NongLiErShiSiJieQi_YangLiYue = laohuangli_dangqianyue[row, 24],

                        NongLiErShiSiJieQi_YangLiRi = laohuangli_dangqianyue[row, 25],

                        NongLiErShiSiJieQi_Shi = laohuangli_dangqianyue[row, 26],

                        NongLiErShiSiJieQi_Fen = laohuangli_dangqianyue[row, 27],

                        NongLiErShiSiJieQi_ZhongQi_JiJie = laohuangli_dangqianyue[row, 28],

                        NongLiErShiSiJieQi_ZhongQi_JieQi = laohuangli_dangqianyue[row, 29],

                        NongLiErShiSiJieQi_ZhongQi_ShiChen = laohuangli_dangqianyue[row,30],

                        NongLiErShiSiJieQi_ZhongQi_YangLiYue = laohuangli_dangqianyue[row,31],

                        NongLiErShiSiJieQi_ZhongQi_YangLiRi = laohuangli_dangqianyue[row,32],

                        NongLiErShiSiJieQi_ZhongQi_Shi = laohuangli_dangqianyue[row, 33],

                        NongLiErShiSiJieQi_ZhongQi_Fen = laohuangli_dangqianyue[row, 34]

                    }

                  );

                }

                    return View("Dangqianyue", laohuangli_models_dangqianyue);

            }

    }

    }

    2019年10月20日 8:01

全部回复

  • 1. 程序调试时发生错误,多次修改该段代码都失败,二维数组值laohuangli_dangqianyue每一行的个数与Laohuangli_Models个数相同,求解决。错误提示如下主要为:

    “/”应用程序中的服务器错误。

    索引超出了数组界限。 

    说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。 源错误: 

    89:             for (int row = 0; row <= laohuangli_dangqianyue.GetLength(0); row++)

    90:             {91:                 laohuangli_models_dangqianyue.Laohuangli_Models_Dangqianyue_.Add(92:                    new Laohuangli_Models()

    93:                    {

    源文件: G:\网络技术\temp\2019.5.1\suwenhua\suwenhua\Controllers\HomeController.cs    : 91 堆栈跟踪: 

    [IndexOutOfRangeException: 索引超出了数组界限。]

       suwenhua.Controllers.HomeController.Dangqianyue() in G:\网络技术\temp\2019.5.1\suwenhua\suwenhua\Controllers\HomeController.cs:91

       lambda_method(Closure , ControllerBase , Object[] ) +62

       System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14

       System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157

       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27

       System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22

       System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29

       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49

       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32

       System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50

       System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +228

       System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10

       System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10

       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49

       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34

       System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26

       System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100

       System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10

       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49

       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27

       System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13

       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29

       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49

       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36

       System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12

       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22

       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49

       System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26

       System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10

       System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21

       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29

       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49

       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28

       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9

       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9839161

       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50

       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163

    版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.8.3928.0

    2019年10月20日 8:01
  • 经过两天查找终于找到问题:

      for (int row = 0; row <= laohuangli_dangqianyue.GetLength(0); row++)

    修改为:

      for (int row = 0; row <laohuangli_dangqianyue.GetLength(0); row++)

    一时大意。

    2019年10月20日 15:29