asp.net mvc + activerecord保存对象图

发布于 2024-07-11 04:55:50 字数 512 浏览 14 评论 0原文

我正在使用 asp.net mvc,我正在尝试创建一个新的员工,在我的表单中,我使用 Html.DropDown(...) 来显示可供选择的部门列表。

理想情况下,我希望 MVC 找出选择了哪个部门(Id 属性是下拉列表中的值),获取它并将其设置在传入的 Employee 对象中。 相反,我得到一个 null 值,并且我必须使用 Request.Form[...] 自己获取部门。

我在这里看到一个例子: http://blog.rodj.org/archive/2008/03/31/activerecord-the-asp.net-mvc-framework.aspx 但这似乎不适用于 asp.net mvc beta

这是带有经过充分验证的 ORM 的基本 CRUD...真的需要这么难吗?

I'm using asp.net mvc and I'm trying to create a new Employee, in my form I use the Html.DropDown(...) to display a list of Departments to select from.

Ideally I would like MVC to just figure out which Department was selected (Id property is the value in dropdown), fetch it and set it in the incoming Employee object. instead I get a null value and I have to fetch the Department myself using the Request.Form[...].

I saw an example here: http://blog.rodj.org/archive/2008/03/31/activerecord-the-asp.net-mvc-framework.aspx but that doesn't seem to work with asp.net mvc beta

This is basic CRUD with a well-proven ORM.... need it really be so hard?

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

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

发布评论

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

评论(2

你丑哭了我 2024-07-18 04:55:50

ASP.NET MVC 不知道如何将 DepartmentId 表单值转换为 Department.Load(DepartmentId) 调用。 为此,您需要为您的模型实现绑定器。

[ActiveRecord("Employees")]
[ModelBinder(EmployeeBinder]
public class Employee : ActiveRecordBase<Employee>
{
    [PrimaryKey]
    public int EmployeeId
    {
        get;
        set;
    }

    [BelongsTo(NotNull = true)]
    public Department Department
    {
        get;
        set;
    }

    // ...
}

EmployeeBinder 负责将路由/表单数据转换为对象。

public class EmployeeBinder : IModelBinder
{
    #region IModelBinder Members

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // ...

        if (controllerContext.HttpContext.Request.Form.AllKeys.Contains("DepartmentId"))
        {
            // The Department Id was passed, call find
            employee.Department = Department.Find(Convert.ToInt32(controllerContext.HttpContext.Request.Form["DepartmentId"]));
        }
        // ...
    }

    #endregion
}

有了这个,只要将 Employee 用作操作的参数,就会调用活页夹。

public ActionResult Create(Employee employee)
    {
        // Do stuff with your bound and loaded employee object!
    }

请参阅 此博文了解更多信息

ASP.NET MVC does not know how to translate the DepartmentId form value to a Department.Load(DepartmentId) call. To do this you need to implement a binder for your model.

[ActiveRecord("Employees")]
[ModelBinder(EmployeeBinder]
public class Employee : ActiveRecordBase<Employee>
{
    [PrimaryKey]
    public int EmployeeId
    {
        get;
        set;
    }

    [BelongsTo(NotNull = true)]
    public Department Department
    {
        get;
        set;
    }

    // ...
}

The EmployeeBinder is responsible for turning route/form data into an object.

public class EmployeeBinder : IModelBinder
{
    #region IModelBinder Members

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // ...

        if (controllerContext.HttpContext.Request.Form.AllKeys.Contains("DepartmentId"))
        {
            // The Department Id was passed, call find
            employee.Department = Department.Find(Convert.ToInt32(controllerContext.HttpContext.Request.Form["DepartmentId"]));
        }
        // ...
    }

    #endregion
}

With this in place anytime an Employee is used as a parameter for an action the binder will be called.

public ActionResult Create(Employee employee)
    {
        // Do stuff with your bound and loaded employee object!
    }

See This blog post for further information

想挽留 2024-07-18 04:55:50

将 ARFetch 移植到 ASP.NET MVC几周前...也许这可以帮助你。

I ported ARFetch to ASP.NET MVC a couple of weeks ago... maybe that could help you.

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