这是我的控制器,在select new
我应该使用什么?如果我使用任何类名,它会显示错误,如名称在此模型中找不到?
这里是控制器代码:
public class HomeController : Controller
{
Database1Entities db = new Database1Entities();
public ActionResult Index()
{
return View();
}
public ActionResult details()
{
var myParts = (from p in db.emps
join d in db.depts on p.Eid equals d.Eid
select new
{
name =p.name,
address = p.address,
number=d.number,
sub=d.sub
}).ToList();
return View(myParts);
}
}
}
我的索引页面如果我给出类名或表名,我应该在模型中给出什么?如果我给出类名,那么我有两个类,我在上面的模型中给出了哪个类
这里是这个索引页面代码:
@model IEnumerable<two2.Models.mass>
@{
ViewBag.Title = " Deaprtment name ";
}
<h2>Index</h2>
@{
ViewBag.Title = "Department";
}
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor (model => model.address)
</th>
<th>
@Html.DisplayNameFor (model => model.sub)
</th>
<th>
@Html.DisplayNameFor (model => model.mark)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.address)
</td>
<td>
@Html.DisplayFor(modelItem => item.sub)
</td>
<td>
@Html.DisplayFor(modelItem => item.mark)
</td>
</tr>
}
</table>
和我的观看页面
在索引页面中显示object not found
并在foreach
语句中显示错误
@model two2.Models.mass
@{
ViewBag.Title = "deatils";
}
@using (@Html.BeginForm())
{
@Html.LabelFor(model => model.name)
@Html.TextBoxFor(model => model.name)
<br /><br />
@Html.LabelFor(model => model.address)
@Html.PasswordFor(model => model.address)
<br /><br />
<br /><br />
@Html.LabelFor(model => model.sub)
@Html.PasswordFor(model => model.sub)
<br /><br />
<br /><br />
@Html.LabelFor(model => model.mark)
@Html.PasswordFor(model => model.mark)
<br /><br />
<button>Login</button>
}
我不知道应该使用什么,以便我的类名或表名以及如何在视图中显示
答案 0 :(得分:0)
你应该在Action中使用相同的类名,它在View中使用,如:
constructor(private _router: Router) {
this._router.events.subscribe((event: NavigationError) => {
if (event.error) {
this._router.navigate(['/error']);
}
});
}
如果您在View中使用:
public ActionResult details()
{
var myParts = (from p in db.emps
join d in db.depts on p.Eid equals d.Eid
select two2.Models.mass
{
Eid = p.Eid,
name = p.name,
address = p.address,
number = d.number,
mark = d.mark, // <- you need to pass mark as it is used in View
sub = d.sub
}).ToList();
return View(myParts);
}
类@model IEnumerable<two2.Models.mass>
是一种ViewModel,它应该只包含View中所需的那些字段:
two2.Models.mass