3

我有一个 ASP.NET WebForms 页面,其中包含一个ASPxGridView和一个ObjectDataSource

<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属性和其他属性(Password例如)为空。

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

4

2 回答 2

3

你也可以通过设置来解决这个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 获取原始员工,然后我们将这个原始员工与更新的员工合并。这可以确保未更新的属性不会变为 null - 尽管它需要额外调用数据库:

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();
}
于 2013-10-05T15:11:51.920 回答
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();
}

但是,其他解决方案非常受欢迎!

于 2013-06-18T09:07:58.300 回答