Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 6 của 6
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Hỏi về DB lookup table (read realated data) MVC 5

    Chào các bạn,

    Mình đang ngâm cứu MVC 5, hiện đang vướng ở một vấn đề nho nhỏ như thế này mong các bạn có kinh nghiệm chỉ giúp với.

    Mình có một diagram như thế này:



    Mình đang làm với Controller Computer : quản lý một số thông tin về máy tính

    ~> Một máy tính thì có thông tin về hãng sx, seri của hãng sx đặt, thời gian nhập về kho, chi nhánh, phòng ban, và người được giao máy sử dụng.

    Browser: /Computer/Index:



    Mình đang vướng ở chỗ :

    - Manufacturer hiện số 1, mình muốn nó lấy dữ liệu ManufacturerName bên bảng Manufacturer (HP, Dell, IBM, Apple,...)

    Tương tự:

    - Branch hiển thị: Chi nhánh 1, Chi nhánh 2, Chi nhánh 3,...

    - Department hiện: Accounting, IT, Sale, Marketing,...

    - User hiện: John, Adam, Paul, Eva,....

    View : /Computer/Index.cshtml


    Mã:
    @model IEnumerable<iCMS.Models.Computer> @{        ViewBag.Title = "Index";    }     Index     <p>        @Html.ActionLink("Create New", "Create")    </p>    <table       <tr>            <th>                @Html.DisplayNameFor(model => model.Manufacturer)            </th>            <th>                @Html.DisplayNameFor(model => model.ManufacturerSeri)            </th>            <th>                @Html.DisplayNameFor(model => model.ImportTime)            </th>            <th>                @Html.DisplayNameFor(model => model.Branch)            </th>            <th>                @Html.DisplayNameFor(model => model.Department)            </th>            <th>                @Html.DisplayNameFor(model => model.User)            </th>            <th></th>        </tr>         @foreach (var item in Model)        {            <tr>                <td>                    @Html.DisplayFor(modelItem => item.Manufacturer)                </td>                <td>                    @Html.DisplayFor(modelItem => item.ManufacturerSeri)                </td>                <td>                    @Html.DisplayFor(modelItem => item.ImportTime)                </td>                <td>                    @Html.DisplayFor(modelItem => item.Branch)                </td>                <td>                    @Html.DisplayFor(modelItem => item.Department)                </td>                <td>                    @Html.DisplayFor(modelItem => item.User)                </td>                <td>                    @Html.ActionLink("Edit", "Edit", new { id = item.ComputerId }) |                    @Html.ActionLink("Details", "Details", new { id = item.ComputerId }) |                    @Html.ActionLink("Delete", "Delete", new { id = item.ComputerId })                </td>            </tr>        }     </table>
    Mình có một Context như thế này:





    Mình có xem một số bài trên asp.net/mvc (asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application) nhưng nó khá lung tung mình chưa nắm được phần Models nên không biết tùy biến thế nào để được như yêu cầu trên cả.

    Các bạn giúp mình với nhé !

    Cảm ơn các bạn !

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mình ko hiểu câu hỏi của bạn lắm. Có phải bạn muốn hiển thị dữ liệu trong bảng Computer ra ngoài. Nhưng thay vì hiển thị dữ liệu cột Manufacturer (là ManufacturerId trong bảng Manufacturer) thì hiển thị ManufacturerName?

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi tuandangn
    Mình ko hiểu câu hỏi của bạn lắm. Có phải bạn muốn hiển thị dữ liệu trong bảng Computer ra ngoài. Nhưng thay vì hiển thị dữ liệu cột Manufacturer (là ManufacturerId trong bảng Manufacturer) thì hiển thị ManufacturerName?
    Chính xác như bạn nói đó ! Bạn giúp mình với nhé ?

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trước hết, muốn hiển thị dữ liệu ra View bạn cần phải có Model chứa dư liệu cần hiểu thị (không nhất định phải có các property tương ứng với từng cột trong database). Bạn tạo Model trong Controller rồi truyền nó đến View:
    Demo cho bạn thử nha. Nếu ko đc thì gửi source qua email cho mình, mình demo cho bạn cái khác: nguyentuandang7@gmail.com

    //model
    public class ComputerModel{
    public int ComputerId{get;set;}
    public int Manufacturer{get;set;}
    public string ManufacturerName{get;set;}
    //property khác
    }


    // controller
    public class ComputerController{
    public ActionResult ComputerList(){
    var model = new List<ComputerModel>();
    var context = new iCMSContext();
    foreach(var com in context.Computers) {
    // nếu có com.Manufacturer
    string manufacturerName = com.Manufacturer.Name;

    // nếu ko có com.Manufacturer
    /*
    string manufacturerName = string.Empty;
    var manufacturer = context.Manufacturers.FirstOrDefault(m => m.ManufacturerId == com.Manufacturer);
    if(manufacturer != null) {
    manufacturerName = manufacturer.ManufacturerName;
    }
    */
    model.Add(new ComputerModel{
    ManufacturerName = manufacturerName,
    ManufactererSeri = com.ManufactererSeri,
    ImportTime= com.ImportTime,
    // các property khác
    });
    }
    return View(model);
    }
    }



    // view ComputerList.cshtml
    @model List<ComputerModel>

    @{
    ViewBag.Title = "Index";
    }

    Index

    <p>
    @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
    <tr>
    <th>
    @Html.DisplayNameFor(model => model.Manufacturer)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.ManufacturerSeri)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.ImportTime)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.Branch)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.Department)
    </th>
    <th>
    @Html.DisplayNameFor(model => model.User)
    </th>
    <th></th>
    </tr>

    @foreach (var item in Model)
    {
    <tr>
    <td>
    @Html.DisplayFor(modelItem => item.Manufacturer)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.ManufacturerSeri)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.ImportTime)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.Branch)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.Department)
    </td>
    <td>
    @Html.DisplayFor(modelItem => item.User)
    </td>
    <td>
    @Html.ActionLink("Edit", "Edit", new { id = item.ComputerId }) |
    @Html.ActionLink("Details", "Details", new { id = item.ComputerId }) |
    @Html.ActionLink("Delete", "Delete", new { id = item.ComputerId })
    </td>
    </tr>
    }

    </table>

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi tuandangn
    Trước hết, muốn hiển thị dữ liệu ra View bạn cần phải có Model chứa dư liệu cần hiểu thị (không nhất định phải có các property tương ứng với từng cột trong database). Bạn tạo Model trong Controller rồi truyền nó đến View:
    Demo cho bạn thử nha. Nếu ko đc thì gửi source qua email cho mình, mình demo cho bạn cái khác: nguyentuandang7@gmail.com
    màu đỏ : cái này làm mình vỡ ra nhiều thứ ! Trước giờ mình cứ nghĩ là phải ánh xạ 1-1 từ db [IMG]images/smilies/clap_grin.gif[/IMG]

    Mã:
    //model
    public class ComputerModel{
       public int ComputerId{get;set;}
       public int Manufacturer{get;set;}
       public string ManufacturerName{get;set;}
      //property khác
    }
    
    
    // controller
    public class ComputerController{
       public ActionResult ComputerList(){
         var model = new List<ComputerModel>();
         var context = new iCMSContext();
         foreach(var com in context.Computers) {
             // nếu có com.Manufacturer
             string manufacturerName = com.Manufacturer.Name;
             
             // nếu ko có com.Manufacturer
             /*
             string manufacturerName = string.Empty;
             var manufacturer = context.Manufacturers.FirstOrDefault(m => m.ManufacturerId == com.Manufacturer);
             if(manufacturer != null) {
              manufacturerName = manufacturer.ManufacturerName;
            }
              */
             model.Add(new ComputerModel{
                 ManufacturerName = manufacturerName,
                 ManufactererSeri = com.ManufactererSeri,
                 ImportTime= com.ImportTime,
                 // các property khác
             });
         }
         return View(model);
      }
    }
    Mã:
    // view ComputerList.cshtml
    @model List<ComputerModel>
    
    @{
            ViewBag.Title = "Index";
        }
    
        Index
    
        <p>
            @Html.ActionLink("Create New", "Create")
        </p>
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Manufacturer)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ManufacturerSeri)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ImportTime)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Branch)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Department)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.User)
                </th>
                <th></th>
            </tr>
    
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Manufacturer)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ManufacturerSeri)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ImportTime)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Branch)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Department)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.User)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.ComputerId }) |
                        @Html.ActionLink("Details", "Details", new { id = item.ComputerId }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.ComputerId })
                    </td>
                </tr>
            }
    
        </table>
    Bạn ơi, mình chưa hiểu chỗ "// nếu có com.Manufacturer" lắm ??

    Cám ơn bạn !

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mình đã làm được rồi !

    Cảm ơn bạn nhé !

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •