如何使用 DefaultModelBinder 绑定模型属性 - ASP.NET MVC2

发布于 2024-11-29 13:41:45 字数 1040 浏览 4 评论 0原文

我有以下场景。

  1. 我的“编辑/员工”视图填充了来自实体框架实体(员工)的模型,
  2. 我从“编辑/员工”发布到“保存/员工”控制器操作。 Save/Employee 操作需要另一种具有 Employee 作为属性的类型 (EmployeeSave)

这是 Edit/Employee 方法

    public ActionResult Edit(EmployeesEdit command)
    {
        var employee = command.Execute();
        if (employee != null)
        {
            return View(employee);
        }
        return View("Index");
    }

这是 Save/Employee 方法

  public ActionResult Save(EmployeesSave command)
    {
        var result = command.Execute();
        if (result)
        {
            return View(command.Employee);
        }
        return View("Error");
    }

这是 EmployeeSave 类

public class EmployeesSave
{
    public bool Execute()
    {
        // ... save the employee   
        return true;

    }
    //I want this prop populated by my model binder
    public Employee Employee { get; set; }  
}

MVC DefaultModelBinder 能够解析 Employee 和 EmployeeSave 类。

I have the following scenario.

  1. I have the Edit/Employee view populated with a model from an Entity Framework entity (Employee)
  2. I post from Edit/Employee to the Save/Employee controller action. The Save/Employee action expect another type (EmployeeSave) which has Employee as property

This is the Edit/Employee method

    public ActionResult Edit(EmployeesEdit command)
    {
        var employee = command.Execute();
        if (employee != null)
        {
            return View(employee);
        }
        return View("Index");
    }

This is the Save/Employee method

  public ActionResult Save(EmployeesSave command)
    {
        var result = command.Execute();
        if (result)
        {
            return View(command.Employee);
        }
        return View("Error");
    }

This is the EmployeeSave class

public class EmployeesSave
{
    public bool Execute()
    {
        // ... save the employee   
        return true;

    }
    //I want this prop populated by my model binder
    public Employee Employee { get; set; }  
}

The MVC DefaultModelBinder is able to resolve both Employee and EmployeeSave classes.

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

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

发布评论

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

评论(2

白云悠悠 2024-12-06 13:41:45

您可能需要在此处使用BindAttribute。如果您的视图包含像这样命名的 EmployeeSaveViewModelEmployee 的属性(我编写了属性名称)

<input type="text" name="EmployeeSaveViewModel.Property1" />
<input type="text" name="EmployeeSaveViewModel.Employee.Name" />
<input type="text" name="EmployeeSaveViewModel.Employee.SomeProperty" />

那么,您的操作可能如下所示:

[HttpPost]
public ActionResult Save([Bind(Prefix="EmployeeSaveViewModel")] 
                         EmployeeSaveViewModel vm)
{
    if(ModelState.IsValid)
    {
        // do something fancy
    }

    // go back to Edit to correct errors
    return View("Edit", vm);
}

You might need to use BindAttribute here. If your view contains the properties of the EmployeeSaveViewModel and Employee named like this (I made up property names)

<input type="text" name="EmployeeSaveViewModel.Property1" />
<input type="text" name="EmployeeSaveViewModel.Employee.Name" />
<input type="text" name="EmployeeSaveViewModel.Employee.SomeProperty" />

Then, your action could look like this:

[HttpPost]
public ActionResult Save([Bind(Prefix="EmployeeSaveViewModel")] 
                         EmployeeSaveViewModel vm)
{
    if(ModelState.IsValid)
    {
        // do something fancy
    }

    // go back to Edit to correct errors
    return View("Edit", vm);
}
陌伤ぢ 2024-12-06 13:41:45

您可以通过将编辑后的数据传递回处理 HttpPost 的编辑操作来解决此问题。在内部创建 EmployeeSave 对象并将其 Employee 属性分配给您的 Edit 操作返回的 Employee 值。通过传递 EmployeeSave 对象来调用 Save 操作。

[HttpGet]
public ActionResult Edit()
{
    return View();
}

[HttpPost]
public ActionResult Edit(Employee employee)
{
    EmployeeSave employeeSave = new EmployeeSave { Employee = employee };

    return View("Save", employeeSave);
}

另一种方法是使用 EmployeeSave 而不是 Employee 作为模型。

You could resolve it by passing the edited data back to Edit action that handles HttpPost. Inside create EmployeeSave object and assign its Employee property the value of Employee returned to yout Edit action. Call Save action by passing EmployeeSave object.

[HttpGet]
public ActionResult Edit()
{
    return View();
}

[HttpPost]
public ActionResult Edit(Employee employee)
{
    EmployeeSave employeeSave = new EmployeeSave { Employee = employee };

    return View("Save", employeeSave);
}

Another method would be to use EmployeeSave instead of Employee as your model.

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