Displaying standard DataTables in MVC

后端 未结 3 1028
别跟我提以往
别跟我提以往 2020-11-27 03:29

Perhaps this is just completely wrong, but back in the days of Webforms you would return a Dataset which you would then bind to a grid. But now in MVC you\'re not supposed

3条回答
  •  旧巷少年郎
    2020-11-27 03:41

    While I tried the approach above, it becomes a complete disaster with mvc. Your controller passing a model and your view using a strongly typed model become too difficult to work with.

    Get your Dataset into a List ..... I have a repository pattern and here is an example of getting a dataset from an old school asmx web service private readonly CISOnlineSRVDEV.ServiceSoapClient _ServiceSoapClient;

        public Get_Client_Repository()
            : this(new CISOnlineSRVDEV.ServiceSoapClient())
        {
    
        }
        public Get_Client_Repository(CISOnlineSRVDEV.ServiceSoapClient serviceSoapClient)
        {
            _ServiceSoapClient = serviceSoapClient;
        }
    
    
        public IEnumerable GetClient(IClient client)
        {
            // ****  Calling teh web service with passing in the clientId and returning a dataset
            DataSet dataSet = _ServiceSoapClient.get_clients(client.RbhaId,
                                                            client.ClientId,
                                                            client.AhcccsId,
                                                            client.LastName,
                                                            client.FirstName,
                                                            "");//client.BirthDate.ToString());  //TODO: NEED TO FIX
    
            // USE LINQ to go through the dataset to make it easily available for the Model to display on the View page
            List clients = (from c in dataSet.Tables[0].AsEnumerable()
                                     select new Client()
                                     {
                                         RbhaId = c[5].ToString(),
                                         ClientId = c[2].ToString(),
                                         AhcccsId = c[6].ToString(),
                                         LastName = c[0].ToString(), // Add another field called   Sex M/F  c[4]
                                         FirstName = c[1].ToString(),
                                         BirthDate = c[3].ToDateTime()  //extension helper  ToDateTime()
                                     }).ToList();
    
            return clients;
    
        }
    

    Then in the Controller I'm doing this

    IClient client = (IClient)TempData["Client"];
    
    // Instantiate and instance of the repository 
    var repository = new Get_Client_Repository();
    // Set a model object to return the dynamic list from repository method call passing in the parameter data
    var model = repository.GetClient(client);
    
    // Call the View up passing in the data from the list
    return View(model);
    

    Then in the View it is easy :

    @model IEnumerable
    
    @{
        ViewBag.Title = "CLIENT ALL INFORMATION";
    }
    
    

    CLIENT ALL INFORMATION

    @foreach (var item in Model) { }
    Last Name First Name Client ID DOB Gender RBHA ID AHCCCS ID
    @Html.ActionLink("Select", "ClientDetails", "Cis", new { id = item.ClientId }, null) | @item.LastName @item.FirstName @item.ClientId @item.BirthDate Gender @* ADD in*@ @item.RbhaId @item.AhcccsId

提交回复
热议问题