ASP.NET MVC - 下拉列表选择 - 部分视图和模型绑定

发布于 2024-10-20 18:15:08 字数 1017 浏览 2 评论 0原文

我对 ASP.NET MVC 相当陌生,正在尝试找出实现此目的的最佳方法。这可能很简单,但我只是想正确地做事,所以我想我会问。

假设我有一个模型是这样的:

任务 - Id、描述、AssignedStaffMember

StaffMember - Id、FirstName、LastName

在我看来,我想创建一个新任务。我创建了一个强类型的 Razor 视图,并且可以使用 EditorFor 为“描述”创建文本框,但是“AssignedStaffMember”又如何呢?

我想要一个所有当前员工的下拉列表,并且可以选择一个,然后将其提交给一个操作方法,该方法是 NewTask(字符串描述,StaffMember 分配的StaffMember) 或者我可以用一个int代表staffId而不是StaffMember对象,然后在action方法中查找它。

最好的方法是什么?我需要去数据库获取员工列表,所以这就是我的想法:

  1. 为员工下拉列表创建一个部分视图,该视图将使用几次并使用@Html。 Action("ListStaff", "Staff") 来调用它。然后动作方法有

    public ActionResult ListStaff()
    {
        IEnumerable<职员>模型 = _serviceLayer.GetAllStaff();
        返回 PartialView(模型);
    }
    

    但是我不确定这将如何与模型绑定一起使用,我的理解是它必须具有正确的表单名称才能提交,我需要将名称传递给部分视图以放置在我猜的元素上?

  2. 不要让它调用控制器来获取人员,而是创建一个包含我的任务和 IEnumerable possibleStaff 集合的 ViewModel。可能会将此信息发送到部分视图。

  3. Html 助手?

  4. 是否可以使用EditorFor?

哪一个(或还有更多)最好?我将如何进行模型绑定?

I'm fairly new to ASP.NET MVC and am trying to work out the best way to do this. It's probably simple but I just want to do things correctly so I thought I'd ask.

Lets say I have a model that is this:

Task - Id, Description, AssignedStaffMember

StaffMember - Id, FirstName, LastName

and in my view I want to create a new task. I make a strongly typed Razor view, and can use EditorFor to create textboxes for Description but what about AssignedStaffMember?

I want a drop down list of all current staff and have the option of selecting one, then this gets submitted to an action method which is
NewTask(string description, StaffMember assignedStaffMember)
either that or I could have an int for staffId instead of the StaffMember object and look it up in the action method.

What is the best way to do this? I need to go to the database to get the list off staff, so here's what I thought:

  1. Make a partial view for the listing of staff drop down, which will be used a few times and use @Html.Action("ListStaff", "Staff") to call it. The action method then has

    public ActionResult ListStaff()
    {
        IEnumerable<StaffMember> model = _serviceLayer.GetAllStaff();
        return PartialView(model);
    }
    

    However I'm not sure on how this will work with model binding, my understanding is that it has to have the correct name for the form to submit it, I'd need to pass the name to the partial view to put on the element I guess?

  2. Instead of having it call a controller to get the staff, make a ViewModel that contains my Task and a IEnumerable possibleStaff collection. possibly send this information to a partial view.

  3. a Html Helper ?

  4. EditorFor could somehow be used?

which one (or is there more) would be best? and how would I do the model binding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

慕烟庭风 2024-10-27 18:15:08

这是执行此操作的一种方法。 创建一个 TaskDetailsViewModel

public class TaskDetailsViewModel
{
    public TaskDetailsViewModel()
    {
        this.Task = new Task();
        this.StaffMembers = new List<StaffMember>();
    }

    public Task Task { get; set; }
    public IEnumerable<StaffMember> StaffMembers { get; set; }
}

在视图中的控制器中

public ActionResult Edit(int id)
{
    var task = taskRepository.GetTaskByID(id);

    var taskDetailsViewModel = new TaskDetailsViewModel();

    // Populate taskDetailsViewModel from task and staff

    return View(taskDetailsViewModel);
}

[HttpPost]
public ActionResult Edit(TaskDetailsViewModel taskDetailsViewModel)
{
    if (ModelState.IsValid)
    {
        taskRepository.Save(taskDetailsViewModel.Task);
    }
    else
    {
        // Show Error
    }

    return View(taskDetailsViewModel);
}

(与 TaskDetailsViewModel 强绑定)

@Html.DropDownListFor(model => model.Task.AssignedStaffMember, new SelectList(Model.StaffMembers, "ID", "FirstName", Model.Task.AssignedStaffMember))
@Html.ValidationMessageFor(model => model.Task.AssignedStaffMember)

Here is one way to do this. Create a TaskDetailsViewModel

public class TaskDetailsViewModel
{
    public TaskDetailsViewModel()
    {
        this.Task = new Task();
        this.StaffMembers = new List<StaffMember>();
    }

    public Task Task { get; set; }
    public IEnumerable<StaffMember> StaffMembers { get; set; }
}

In Controller

public ActionResult Edit(int id)
{
    var task = taskRepository.GetTaskByID(id);

    var taskDetailsViewModel = new TaskDetailsViewModel();

    // Populate taskDetailsViewModel from task and staff

    return View(taskDetailsViewModel);
}

[HttpPost]
public ActionResult Edit(TaskDetailsViewModel taskDetailsViewModel)
{
    if (ModelState.IsValid)
    {
        taskRepository.Save(taskDetailsViewModel.Task);
    }
    else
    {
        // Show Error
    }

    return View(taskDetailsViewModel);
}

In View (bound strongly to TaskDetailsViewModel)

@Html.DropDownListFor(model => model.Task.AssignedStaffMember, new SelectList(Model.StaffMembers, "ID", "FirstName", Model.Task.AssignedStaffMember))
@Html.ValidationMessageFor(model => model.Task.AssignedStaffMember)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文