在 MVC 详细视图中显示值列表
我是 Web 应用程序、MVC 和 LinqToSql 的新手。我使用 NerdDinner 教程作为指南创建了一个 MVC Web 应用程序。我现在正在向其添加多对多关系。我每一步都会碰壁。我在编辑和创建视图中有一个多选列表。对于详细信息和列表视图,我想列出选定的值。
我有 3 个表:Company、Subcontract 和一个链接表 CompanyToSubcontract。我有代码从 CompanyToSubcontract 表中获取我选择的公司的 guid,该表在我的代码中的其他地方使用。我不知道如何显示它。
我应该编写另一个函数来从公司表中获取公司名称吗?我是否将名称列表传递到 SubcontractDetail 视图,然后以某种方式循环访问它?
与 SubcontractIndex 视图相同的问题。索引视图采用表格格式,我想要一个“公司”列,其中每个分包合同行都有一个以逗号分隔的公司列表。
[Authorize]
public ActionResult Details(string id)
{
subcontract subcontract = subcontractRepository.GetSubcontract(id);
IEnumerable<Guid> cmpny = subcontractRepository.GetSubcontractCompanies(subcontract.subcontract_id);
if (subcontract == null)
return View("NotFound");
else
{
return View("Details", subcontract);
}
}
[Authorize]
public ActionResult Index()
{
var subcontracts = subcontractRepository.FindAllSubcontracts().ToList();
return View("Index", subcontracts);
}
I am new to web apps, MVC, and LinqToSql. I created an MVC web app using the NerdDinner tutorial as a guide. I'm now adding many-to-many relationships to it. And am just running into walls at every step. I have a multiselect list in the Edit and Create views. For the Detail and List views I would like to list the selected values.
I have 3 tables: Company, Subcontract, and a link table CompanyToSubcontract. I have code which gets the guid of my selected companies from the CompanyToSubcontract table which is used elsewhere in my code. I do not know how to display it.
Should I write another function to get the Company names from the Company table? Do I pass the list of names to the SubcontractDetail view and then somehow loop through it there?
Same questions with the SubcontractIndex view. The Index view is in table format, I'd like to have a "Company" column which has a comma separated list of the companies for each subcontract row.
[Authorize]
public ActionResult Details(string id)
{
subcontract subcontract = subcontractRepository.GetSubcontract(id);
IEnumerable<Guid> cmpny = subcontractRepository.GetSubcontractCompanies(subcontract.subcontract_id);
if (subcontract == null)
return View("NotFound");
else
{
return View("Details", subcontract);
}
}
[Authorize]
public ActionResult Index()
{
var subcontracts = subcontractRepository.FindAllSubcontracts().ToList();
return View("Index", subcontracts);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有多种选择:
1)您可以创建包含转包和分包的新类; IEnumerable 并将新类的对象传递给视图。这是 ASP.NET MVC 中的常见做法。
在控制器中:
并添加这个新类型应该是您的视图的类型。
2)您也可以将 IEnumerable 添加到 ViewData 集合中,如下所示:
然后使用视图中的索引器访问它
我个人会推荐您第一个,因为您不需要强制转换并且它更干净
You have several options:
1) You can create new class that contains subcontract & IEnumerable and pass an object of the new class to the view. This is common practice in ASP.NET MVC .
And in the controller:
And add this new type should be the type of your view.
2) Also you can add the IEnumerable to the ViewData collection like this:
And then access it with the indexer in the view
I personally will recomand you the firs because you don't need to cast and it is cleaner