代码之家  ›  专栏  ›  技术社区  ›  Mr doubt

多个子字符串在MVC Razor视图中不起作用

  •  0
  • Mr doubt  · 技术社区  · 4 年前

     @foreach (var i in (List<MyDME.Data.Model.ERNFileRequestDTO>)ViewBag.lst)
                        {
                            for (int j = 0; j < i.Parse835Details.storedChkNo.Count; j++)
                            {
    
                                <tr>
                                    <td>
                                        @i.Parse835Details.storedPayorName[j]
                                    </td>
                                    <td>
                                        @i.Parse835Details.storedChkNo[j]
                                    </td>
                                    <td>
                                        @i.Parse835Details.storedTotalBilled[j]
    
                                    </td>
                                    <td>
                                        @i.Parse835Details.storedTotalPaid[j]
    
                                    </td>
                                    <td>
                                  //below line doing substring which is not working
       @(i.Parse835Details.storedChkDate[j].Substring(4, 5) + " " + i.Parse835Details.storedChkDate[j].Substring(6, 7) + " " + i.Parse835Details.storedChkDate[j].Substring(0, 3))
                                       
                                    </td>
                                    <td>
                                        <a href='/PatientManagement/DownloadUploadedDocument?fileName=@Html.Raw(i.path)'>Download</a>
    
                                    </td>
                                </tr>
    

    下面的错误是

    System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Parameter name: length'
    

    20210225 类似于日期格式是以字符串形式出现的,从这个值开始执行子字符串,但不能对多个子字符串执行子字符串。

    @i.Parse835Details.storedChkDate[j].Substring(0, 5)
    

    @i.Parse835Details.storedChkDate[j].Substring(4, 5)
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   Peter B    4 年前

    你正在使用这个方法 public string Substring (int startIndex, int length) .
    public string Substring (int startIndex, int endIndex) .

    提取字符4+5,你需要通过 length 如2所示:

    @i.Parse835Details.storedChkDate[j].Substring(4, 2)