Reputation: 1
This is my ViewModel
namespace CRUD2.ViewModel
{
public class CostVM
{
public int id { get; set;}
public string nama { get; set; }
public string alamat { get; set; }
public string jenis { get; set; }
public informasi informasi { get; set; }
public iseng iseng { get; set; }
}
}
This is my Index.cshtml
@model IEnumerable<CRUD2.ViewModel.CostVM>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table cellspacing="2px;">
<tr>
<th>Nama</th>
<th>Alamat</th>
<th>Jenis</th>
<th>Action</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.informasi.nama</td>
<td>@item.informasi.alamat</td>
<td>@item.iseng.jenis</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.informasi.id }) |
@Html.ActionLink("Back to List", "Index")
</td>
</tr>
}
</table>
</body>
</html>
and this is my Controler (Edit and Index)
namespace CRUD2.Controllers
{
public ActionResult Edit(int id = 0)
{
var costumerlist = from cust in db.informasis
join ord in db.isengs
on cust.id equals ord.id
where cust.id == id
select new { cust.nama, cust.alamat, cust.jk, cust.kelas, ord.jenis };
return View(costumerlist.FirstOrDefault());
}
}
}
How can I make my Edit.cshtml? I dont understand how to make that, I have no idea, and if I have any mistakes in my code, please fix it.. Thanks
Sorry for my Bad English..
Upvotes: 0
Views: 312
Reputation: 50728
You are creating a custom hybrid of two data sources, which is fine. You can add an Edit view with:
@model dynamic
Since you have an anonymous type, but better would be to create a class with all of the properties returned from this select:
public class SomeViewModel
{
public string nama { get; set; }
.
.
}
And then change the query to use this:
public ActionResult Edit(int id = 0)
{
var costumerlist = from cust in db.informasis
join ord in db.isengs
on cust.id equals ord.id
where cust.id == id
select new SomeViewModel { cust.nama, cust.alamat, cust.jk, cust.kelas, ord.jenis };
return View(costumerlist.FirstOrDefault());
}
And then create a new view for edit which will use this model:
@model SomeViewModel
To create an edit view would be the same way you created the Index view; right click on the controller action, select Add View and select the model.
Upvotes: 1