gpt4 book ai didi

c# - 如何将列表从 View 传递到 Controller ASP.NET MVC5

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:54 24 4
gpt4 key购买 nike

我有这些模型:

public class Condominium
{
public int CondominiumID { get; set; }

public int BatchID { get; set; }

public string Name { get; set; }

public bool LiveInCondominium { get; set; }

public string Phone { get; set; }

public string Email { get; set; }

public Batch Batch { get; set; }

public List<Employee> Employees { get; set; }
}

public class Employee
{
public int EmployeeID { get; set; }

public int CityID { get; set; }

public string UserID { get; set; }

public Condominium CondominiumID { get; set; }

public string Name { get; set; }

public string Address { get; set; }

public string ZipCode { get; set; }

public string Contact { get; set; }

public string Phone { get; set; }

public string Email { get; set; }

public City City { get; set; }

public Condominium Condominium { get; set; }
}

我需要创建 Employee动态对象并将它们放入列表中,当我发帖请求对象时 Condominium包含带有 Employee 的列表对象。我不知道如何为此创建 View 。

最佳答案

我建议您为每个 View 构建 View 模型,在这种情况下,您将构建一个模型,其中包含一个包含员工列表的属性。然后,您可以简单地填充模型并将其返回到 View 。

这是一些伪代码:

Controller

public ActionResult Index() 
{
var viewModel = new ViewModel()
{
Employees = BuildListOfEmployees() // Method that gets/builds a list of employees, expected return type List<Employee>
};

return View(viewModel);
}

class ViewModel
{
public List<Employee> Employees { get; set; }
}

查看

@model YourNamespace.ViewModel

<ul>
@foreach (var employee in Model)
{
<li>employee.Name</li>
}
</ul>

关于c# - 如何将列表从 View 传递到 Controller ASP.NET MVC5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com