-
03-12-2013, 09:08 AM #1Junior Member
- 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ó 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 !View more random threads:
- Lỗi asp:UpdatePanel’ could not be set on property ‘ContentTemplate’ trong vs 2008
- ý nghĩ của StringBuilder trong C#?
- Xuống dòng trong 1 Cell trong table
- Cách backup CSDL SQL Server trên Web
- Cách hiển thị các Sản phẩm cùng thể loại, và các tin cùng chủ đề
- Lọc dữ liệu bằng textbox đổ vào Gridview dùng sqlDatasource
- Hỏi: Cách Làm video playlist với video html5
- Tạo chức năng thay đổi giao diện web trên MVC
- Cách lưu Session vào trong database ?
- Làm cách nào để gọi 1 hàm API được xây dựng sẵn
-
03-12-2013, 06:57 PM #2Junior Member
- 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?
-
04-12-2013, 02:16 PM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi tuandangn
-
05-12-2013, 08:46 AM #4Junior Member
- 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>
-
05-12-2013, 10:39 AM #5Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Gửi bởi tuandangn
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>
Cám ơn bạn !
-
05-12-2013, 04:48 PM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Mình đã làm được rồi !
Cảm ơn bạn nhé !
Pallet nhựa Long An đã trở thành một trong những lựa chọn phổ biến cho nhu cầu vận chuyển và lưu trữ hàng hóa trong nhiều ngành công nghiệp. Với đặc tính nhẹ nhàng, chắc chắn và dễ vận chuyển, các...
Thanh lý pallet nhựa Long An giá rẻ