Sql 如何通过将计数值传递到";“返回视图方法”;

Sql 如何通过将计数值传递到";“返回视图方法”;,sql,asp.net-mvc,linq,model-view-controller,razor,Sql,Asp.net Mvc,Linq,Model View Controller,Razor,如何显示表中保留的记录总数(计数)。我想在视图中显示计数,所以我试图在返回视图中将计数作为参数传递,但我得到一个错误,即无法将字符串转换为int。我非常确定有一种更聪明的方法可以做到这一点。我曾尝试使用toString()转换int值,但此后仍然出现语法错误。我已将控制器和视图放置在下面。请注意,我试图在返回视图方法中的控制器中执行的操作,我试图插入计数,但我得到一个错误 无法从int转换?串 控制器 using System; using System.Collections.Ge

如何显示表中保留的记录总数(计数)。我想在视图中显示计数,所以我试图在返回视图中将计数作为参数传递,但我得到一个错误,即无法将字符串转换为int。我非常确定有一种更聪明的方法可以做到这一点。我曾尝试使用toString()转换int值,但此后仍然出现语法错误。我已将控制器和视图放置在下面。请注意,我试图在返回视图方法中的控制器中执行的操作,我试图插入计数,但我得到一个错误 无法从int转换?串

控制器

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Data;
    using System.Web.Http;
    using System.Configuration;
    using PagedList;

    namespace App.Web.Controllers
    {
        public class DisplayUploadedFileController : Controller
        {
            private EntitiesModel db = new EntitiesModel();

            public ActionResult DisplayUploadedFileContents(int? id, int? page, int? totalCount)
            {

                var vm = new dbclients_invalidEmailsVM();
                vm.UploadId = id ?? default(int);
                if (page == null)
                {
                    page = 1;
                }
                int pageSize = 10;
                int pageNumber = (page ?? 1);

                var rows = from myRow in db.tbl_dataTable
                           select myRow;
                totalCount = rows.Count();

                return View(db.tbl_dataTable.OrderByDescending(r => r.ClientId).Where(r => r.UploadId == id).ToList().ToPagedList(pageNumber, pageSize), totalCount);

            }
        }

}
查看

@model PagedList.IPagedList<App.Web.marketingdbclients_dataTable>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link href="http://www.jqueryscript.net/css/jquerysctipttop.css" rel="stylesheet" type="text/css">
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="pagination.js"></script>
</head>
<body>
    <div class="container" style="margin-top:50px;">

        <table class="table" id="table">
            <tr>
                <th>
                    First Name
                </th>

                <th>
                    Last Name
                </th>
                <th>
                    Cell1
                </th>
                <th>
                    Email1
                </th>

                <th>
                    Company
                </th>
                <th>
                    Job Title
                </th>
                <th>
                    Province
                </th>

                <th>
                    Source
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>

                    <td>
                        @Html.DisplayFor(modelItem => item.FirstName)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.LastName)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.Cell1)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.Email1)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Company)
                    </td>

                    <td>
                        @Html.DisplayFor(modelItem => item.JobTitle)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.PhysicalProvince)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Source)
                    </td>



                </tr>
            }

        </table>
        </br>
        Number of Records  @Model.Count()<br />

        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
        @Html.PagedListPager(Model, page => Url.Action("DisplayUploadedFileContents", new { uploadId = Model.First().UploadId, page }))
    </div>
</body>
</html>


My Table Represented as a model

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

    namespace App.Web
    {
        using System;
        using System.Collections.Generic;

        public partial class marketingdbclients_dataTable
        {
            public int ClientDataId { get; set; }
            public Nullable<int> ClientId { get; set; }
            public Nullable<int> UploadId { get; set; }
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastName { get; set; }
            public string IdentificationNumber { get; set; }
            public string RaceId { get; set; }
            public string DateOfBirth { get; set; }
            public string Age { get; set; }
            public string TitleTypeId { get; set; }
            public string GenderTypeId { get; set; }
            public string Nationality { get; set; }
            public string PhysicalCountry { get; set; }
            public string PhysicalProvince { get; set; }
            public string PhysicalCity { get; set; }
            public string Area { get; set; }
            public string HighestQualification { get; set; }
            public string CurrentQualification { get; set; }
            public string PhysicalAddress { get; set; }
            public string PostalAddress { get; set; }
            public string Cell1 { get; set; }
            public string Cell2 { get; set; }
            public string Cell3 { get; set; }
            public string Cell4 { get; set; }
            public string Work1 { get; set; }
            public string Work2 { get; set; }
            public string Work3 { get; set; }
            public string Work4 { get; set; }
            public string Home1 { get; set; }
            public string Home2 { get; set; }
            public string Home3 { get; set; }
            public string Home4 { get; set; }
            public string LSMGroup { get; set; }
            public string Municipality { get; set; }
            public string Crediting_Rating { get; set; }
            public string Email1 { get; set; }
            public string Email2 { get; set; }
            public string Email3 { get; set; }
            public string Email4 { get; set; }
            public string Income { get; set; }
            public string Company { get; set; }
            public string Industry { get; set; }
            public string JobTitle { get; set; }
            public string LeadStage { get; set; }
            public string ReggieNumber { get; set; }
            public string Source { get; set; }
            public System.DateTime DateInserted { get; set; }
            //public int totalEntriesCount { get; set; }
        }
    }
@model PagedList.IPagedList
@使用PagedList.Mvc;
名字
姓
第1单元
电子邮件1
单位
职位名称
省
来源
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.FirstName)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modeleItem=>item.Cell1)
@DisplayFor(modelItem=>item.Email1)
@DisplayFor(modelItem=>item.Company)
@DisplayFor(modelItem=>item.JobTitle)
@DisplayFor(modelItem=>item.PhysicalProvince)
@DisplayFor(modelItem=>item.Source)
}

记录数@Model.Count()
@Model.PageCount的@页(Model.PageCountUrl.Action(“DisplayUploadedFileContents”,new{uploadId=Model.First().uploadId,page})) 我的桌子用模型表示 //------------------------------------------------------------------------------ // //此代码是从模板生成的。 // //手动更改此文件可能会导致应用程序出现意外行为。 //如果重新生成代码,将覆盖对此文件的手动更改。 // //------------------------------------------------------------------------------ 名称空间App.Web { 使用制度; 使用System.Collections.Generic; 公共部分类marketingdbclients\u数据表 { public int ClientDataId{get;set;} 公共可为空的ClientId{get;set;} 公共可为空的上载ID{get;set;} 公共字符串名{get;set;} 公共字符串MiddleName{get;set;} 公共字符串LastName{get;set;} 公共字符串标识号{get;set;} 公共字符串RaceId{get;set;} 公共字符串DateOfBirth{get;set;} 公共字符串年龄{get;set;} 公共字符串TitleTypeId{get;set;} 公共字符串GenderTypeId{get;set;} 公共字符串{get;set;} 公共字符串PhysicalCountry{get;set;} 公共字符串物理省{get;set;} 公共字符串物理性{get;set;} 公共字符串区域{get;set;} 公共字符串最高级限定{get;set;} 公共字符串CurrentQualification{get;set;} 公共字符串物理地址{get;set;} 公共字符串PostalAddress{get;set;} 公共字符串Cell1{get;set;} 公共字符串Cell2{get;set;} 公共字符串Cell3{get;set;} 公共字符串Cell4{get;set;} 公共字符串Work1{get;set;} 公共字符串Work2{get;set;} 公共字符串Work3{get;set;} 公共字符串Work4{get;set;} 公共字符串Home1{get;set;} 公共字符串Home2{get;set;} 公共字符串Home3{get;set;} 公共字符串Home4{get;set;} 公共字符串LSMGroup{get;set;} 公共字符串{get;set;} 公共字符串贷记_评级{get;set;} 公共字符串Email1{get;set;} 公共字符串Email2{get;set;} 公共字符串Email3{get;set;} 公共字符串Email4{get;set;} 公共字符串收入{get;set;} 公共字符串公司{get;set;} 公共字符串行业{get;set;} 公共字符串JobTitle{get;set;} 公共字符串LeadStage{get;set;} 公共字符串ReggieNumber{get;set;} 公共字符串源{get;set;} public System.DateTime DateInserted{get;set;} //公共int totalEntriesCount{get;set;} } }
请创建一个新的ViewModel类,并按如下方式存储两个输入:

public class MyViewModel
{
    public List<marketingdbclients_dataTable> marketingdbclients_dataTables { get; set; }
    public int totalCount { get; set; }

    public MyViewModel()
    {
        this.marketingdbclients_dataTables = new List<marketingdbclients_dataTable>();
        this.totalCount = 0;
    }
}
然后在视图(index.cshtml)中声明MyViewModel,如下所示:

@model WebApp.Models.MyViewModel

<div>
    your html
</div>
@model WebApp.Models.MyVie
@model WebApp.Models.MyViewModel

<div>
    your html
</div>
 return View(
  new {
    Page = db.tbl_dataTable.OrderByDescending(r => r.ClientId).Where(r => r.UploadId == id).ToList().ToPagedList(pageNumber, pageSize)db.tbl_dataTable.OrderByDescending(r => r.ClientId).Where(r => r.UploadId == id).ToList().ToPagedList(pageNumber, pageSize),
    Count = totalCount
 });