Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

show dữ liệu từ datatable ra View trong asp.net mvc

8 năm trước 2.085 lượt xem 3 bình luận 8 năm trước

Mình có 1 datatable đã lấy được dữ liệu(Mô hình asp.net MVC).
giờ làm sao để lấy được dữ liệu từ datatable đó đổ dữ liệu ra View đc ạ

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Nguyen Van Tung đã bình luận 8 năm trước
Em đã sửa lại rồi
Controller:

public ActionResult Index(ReportMerchantSearchForm searchForm, string command)
        {
            if (command.Equals("Search"))
            {
                var model = new ReportMerchantViewModel();
                string getMerchantPurse = "";
                var mcDao = new mc_merchantsDao();
                var merchantlist = mcDao.ListAll();
                foreach (var item in merchantlist)
                {
                    getMerchantPurse += "'" + item.merchant_purse + "',";
                }
                getMerchantPurse = getMerchantPurse.Substring(0, getMerchantPurse.Length - 1);
                var wm_devbackend_operationsDao = new wm_devbackend_operationsDao();
                DataTable historyMerchant = wm_devbackend_operationsDao.getHistoryMerchant(getMerchantPurse, CommonConstants.CONNECTION_STRING_WALLET_DB);
                return View(model);
            }
            return View();
        }

View:

@model List<WM.DAL.ViewModel.ReportMerchantViewModel>

foreach (var item in Model)
    {
        <div>@item.comiss</div>
    }

 

Mà sau khi chạy lại báo lỗi này ạ:

The model item passed into the dictionary is of type 'WM.DAL.ViewModel.ReportMerchantViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[WM.DAL.ViewModel.ReportMerchantViewModel]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'WM.DAL.ViewModel.ReportMerchantViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[WM.DAL.ViewModel.ReportMerchantViewModel]'.

Source Error: 
 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

 

Nguyen Van Tung đã bình luận 8 năm trước

Có ai đang onl support dùm em với ạ

Kteam SuperAdmin đã bình luận 8 năm trước

bên view @model Kiểu dữ liệu

Bên controller return View(model của bạn);

Bài viết
0
Câu hỏi
10
Bình luận
15