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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5188563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:57:37  来源:igfitidea点击:

ASP.NET MVC - drop down list selection - partial views and model binding

asp.net-mvcrazorpartial-viewsmodel-binding

提问by RodH257

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.

我对 ASP.NET MVC 还很陌生,我正在尝试找出最好的方法来做到这一点。这可能很简单,但我只想正确地做事,所以我想我会问。

Lets say I have a model that is this:

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

Task - Id, Description, AssignedStaffMember

任务 - Id、描述、AssignedStaffMember

StaffMember - Id, FirstName, LastName

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?

在我看来,我想创建一个新任务。我创建了一个强类型的 Razor 视图,并且可以使用 EditorFor 为 Description 创建文本框,但是 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.

我想要一个所有当前员工的下拉列表,并可以选择一个,然后将其提交给一个操作方法,该方法 NewTask(string description, StaffMember assignedStaffMember)要么是那个,要么我可以为 staffId 而不是 StaffMember 对象设置一个 int 并在操作中查找它方法。

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?

  1. 为员工列表下拉做局部视图,会用到几次,@Html.Action("ListStaff", "Staff")用来调用。动作方法然后有

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

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

  2. 与其调用控制器来获取人员,不如创建一个包含我的 Task 和一个 IEnumerable possibleStaff 集合的 ViewModel。可能将此信息发送到局部视图。

  3. 一个 HTML 助手?

  4. EditorFor 可以以某种方式使用吗?

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

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

回答by amit_g

Here is one way to do this. Create a TaskDetailsViewModel

这是执行此操作的一种方法。创建一个 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)

在视图中(强烈绑定到 TaskDetailsViewModel)

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