我是C#的新手,特别是在MVC中。 我想用这样的模型返回一个视图:
public ActionResult Index()
{
return View(db.MyModel.Where(a => a.EmployeID == m_idEmployee));
}
针对特定员工(其中m_idEmployee
是员工/用户的id
,但目前,这是我自己设定的值)
我不使用visual studio中的loggingaccount
方法(对我来说不是很清楚)
所以我想知道我是否可以“传递”索引中的值来设置m_idEmployee
(例如,从我的视图中)。
答案 0 :(得分:3)
您可以将员工ID传递给Index方法,如:
public ActionResult Index(int empId)
{
return View(db.MyModel.Where(a => a.EmployeID == empId));
}
你必须通过将其作为查询字符串传递或者将动词更改为post来更改调用Index方法的方式; model binder
会处理它。
Modelbinder快速参考:
将浏览器请求映射到数据对象。这个班提供了一个 具体实现模型绑定器。
答案 1 :(得分:1)
将您的控制器方法更改为:
public ActionResult Index(int employeeId)
然后在您的视图中创建一个操作:
@Html.ActionLink("Text of the link", "Index", "NameOfYourController", new { employeeId = 123 })
答案 2 :(得分:1)
您可以在类Independente
中创建Class返回值并在控制器类中调用此函数
EmployerModel.class
public findEmployerById(int id){
return db.MyModel.Where(a => a.EmployeID == id);
}
public ActionResult Index(int id)
{
return View(new EmployerModel().findEmployerById(id));
}
在您的视图中,您需要添加
@model package.MyModel