这是我的控制器,在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中输入什么如果我输入类名或表名如果我输入类名那么我有两个类我在上面输入的类model
这里这个索引页代码:
@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>
}
我不知道我应该用什么来表示我的类名或表名以及如何在视图上显示
在Action中也应该使用与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);
}
如果你使用的是in View:
@model IEnumerable<two2.Models.mass>
类two2.Models.mass
是一种类型的ViewModel,它应该只包含视图中需要的那些字段:
namespace two2.Models
{
public class mass
{
public int Eid { get; set; }
public string name { get; set; }
public string address { get; set; }
public int number { get; set; }
public string sub { get; set; }
public int mark { get; set; }
}
}