GridView与ObjectDatasource UpdateMethod

时间:2013-06-17 21:13:50

标签: c# asp.net entity-framework devexpress objectdatasource

我有一个包含ASPxGridViewObjectDataSource的ASP.NET WebForms页面:

<dx:ASPxGridView ID="gvEmployees" runat="server"
    AutoGenerateColumns="False" DataSourceID="objDsEmployees"
    KeyFieldName="EmployeeId">
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0">
            <EditButton Visible="True" />
        </dx:GridViewCommandColumn>
        <dx:GridViewDataTextColumn FieldName="EmployeeId" VisibleIndex="1" />
        <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="2" />
        <dx:GridViewDataTextColumn FieldName="Email" VisibleIndex="3" />
        <dx:GridViewDataTextColumn FieldName="Telephone" VisibleIndex="5" />
    </Columns>
</dx:ASPxGridView>
<asp:ObjectDataSource ID="objDsEmployees"
    runat="server"
    ConflictDetection="CompareAllValues"
    DataObjectTypeName="MySite.Data.Models.Employee"
    DeleteMethod="Delete"
    OldValuesParameterFormatString="original{0}"
    InsertMethod="Insert"
    SelectMethod="GetAll"
    TypeName="MySite.Services.EmployeeService"
    UpdateMethod="Update" />

Employee模型包含以下属性:

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

    public string Name { get; set; }

    public string Email { get;

    public string Password { get; set; }

    public string Telephone { get; set; }
}

ObjectDataSource调用服务层中的Update方法:

public void Update(Employee employee, Employee originalEmployee)
{
    _db.Employees.Attach(originalEmployee);
    _db.Entry(originalEmployee).CurrentValues.SetValues(employee);
    _db.SaveChanges();
}

调用Update方法时,参数employeeoriginalEmployee仅包含ASPxGridView中使用的属性以及其他属性({{1}例如)是null。

有没有办法可以在某处保留这些值? 顺便说一下,我使用Entity Framework进行数据访问,使用DevExpress进行GridView。

2 个答案:

答案 0 :(得分:2)

您也可以在ConflictDetection="CompareAllValues"

中设置ObjectDataSource来解决此问题
<asp:ObjectDataSource ID="objDsEmployees"
    runat="server"
    DeleteMethod="Delete"
    InsertMethod="Insert"
    SelectMethod="GetAll"
    UpdateMethod="Update"
    DataObjectTypeName="App.Core.Domain.Employee"
    TypeName="App.Core.Services.EmployeeService"
    OldValuesParameterFormatString="original{0}"
    ConflictDetection="CompareAllValues" />

然后在Update中的EmployeeService方法中,我们首先通过其ID获取原始员工,然后我们将此原始员工与更新后的员工合并。这可以确保未更新的属性不会变为空 - 尽管它需要额外调用数据库:

public void Update(Employee originalEmployee, Employee employee)
{
    // Get the original employee by its id.
    Employee fullOriginalEmployee = GetById(originalEmployee.EmployeeId);

    // Merge the data of the updated employee with the original employee.
    fullOriginalEmployee.Name = employee.Name;
    fullOriginalEmployee.Email = employee.Email;
    fullOriginalEmployee.Telephone = employee.Telephone;
    fullOriginalEmployee.BirthDate = employee.BirthDate;

    // Persist changes in database.
    SaveChanges();
}

答案 1 :(得分:0)

我通过将ObjectDataSource更改为:

解决了这个问题
<asp:ObjectDataSource 
    ID="objDsEmployees"
    runat="server"
    DeleteMethod="Delete"
    InsertMethod="Insert"
    SelectMethod="GetAll"
    TypeName="MySite.Services.EmployeeService"
    UpdateMethod="Update">
    <UpdateParameters>
        <asp:Parameter Name="employeeId" Type="Int32" />
        <asp:Parameter Name="name" Type="String" />
        <asp:Parameter Name="email" Type="String" />
    </UpdateParameters>
</asp:ObjectDataSource>

服务层中的Update方法为:

public void Update(int employeeId, string name, string email)
{
    var employee = GetById(employeeId);
    employee.Name = name;
    employee.Email = email;

    _db.SaveChanges();
}

然而,其他解决方案非常受欢迎!