在 MVC 详细视图中显示值列表

发布于 2024-08-19 14:32:43 字数 1094 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2024-08-26 14:32:43

您有多种选择:
1)您可以创建包含转包和分包的新类; IEnumerable 并将新类的对象传递给视图。这是 ASP.NET MVC 中的常见做法。

public class MyCustomModelView
{
    public IEnumerable<Guid> Company{ get; set; }
    public subcontract Subcontract { get; set; }
}

在控制器中:

return View("Details", new MyCustomModelView(){
    Company = cmpny, 
    Subcontract = subcontract });

并添加这个新类型应该是您的视图的类型。

enter code here

2)您也可以将 IEnumerable 添加到 ViewData 集合中,如下所示:

ViewData["Company"] = cmpny;

然后使用视图中的索引器访问它

<% foreach(Guid id in (ViewData["Company"] as IEnumerable<Guid>)) {  %>
...Display names...
<% } %>

我个人会推荐您第一个,因为您不需要强制转换并且它更干净

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 .

public class MyCustomModelView
{
    public IEnumerable<Guid> Company{ get; set; }
    public subcontract Subcontract { get; set; }
}

And in the controller:

return View("Details", new MyCustomModelView(){
    Company = cmpny, 
    Subcontract = subcontract });

And add this new type should be the type of your view.

enter code here

2) Also you can add the IEnumerable to the ViewData collection like this:

ViewData["Company"] = cmpny;

And then access it with the indexer in the view

<% foreach(Guid id in (ViewData["Company"] as IEnumerable<Guid>)) {  %>
...Display names...
<% } %>

I personally will recomand you the firs because you don't need to cast and it is cleaner

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文