【问题标题】:GridView with ObjectDatasource UpdateMethod带有 ObjectDatasource 更新方法的 GridView
【发布时间】:2013-06-17 21:13:49
【问题描述】:

我有一个 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。

【问题讨论】:

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


    【解决方案1】:

    您也可以通过在ObjectDataSource 中设置ConflictDetection="CompareAllValues" 来解决此问题:

    <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" />
    

    然后在 EmployeeServiceUpdate 方法中,我们首先通过其 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();
    }
    

    【讨论】:

    • 你拯救了我的一天!
    【解决方案2】:

    我通过将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();
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2014-02-21
      • 1970-01-01
      • 2013-07-29
      相关资源
      最近更新 更多