预填表格导致DbContext.SaveChanges()

时间:2018-01-07 06:06:11

标签: c# asp.net entity-framework linq

经过大量的调试后,我已经缩小了为什么我的dbContext更新不起作用。

我有一个在Page_Load上运行的脚本,它将根据查询字符串category_Id填充表单,这是我表的主键。

protected void Page_Load(object sender, EventArgs e)
{
        // Populate Edit Fields
        if (Request.QueryString["categoryId"] != null)
        {
            CategoryDAL categoryDAL = new CategoryDAL();
            RecipeCategory myCategory = new RecipeCategory();

            try
            {
                addDiv.Attributes["class"] = "hidden";
                editDiv.Attributes["class"] = "display";

                int categoryToGet = Convert.ToInt32(Request.QueryString["categoryId"]);
                myCategory = categoryDAL.GetCategory(categoryToGet);

                tbEditCategoryName.Text = myCategory.Category_Name;
                tbEditCategoryDescription.Text = myCategory.Description;

                ddlEditCategoryGroupList.SelectedValue = Convert.ToString(myCategory.CatGroup_Id);
                ddlEditCategoryGroupList.DataBind();
            }
            catch(Exception ex)
            {
                updateStatus.Attributes["class"] = "alert alert-info alert-dismissable fade in";
                updateStatus.Visible = true;
                lblStatus.Text = "Could not get Category Info, please try again.";
            }
        }

这是我单击edit_Button时运行的脚本,它应该更新数据库中的行并重定向到viewCategories页面。

    protected void btnEditCategory_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["categoryId"] != null)
        {
            CategoryDAL categoryDAL = new CategoryDAL();
            RecipeCategory myCategory = new RecipeCategory();

            try
            {
                int categoryToEdit = Convert.ToInt32(Request.QueryString["categoryId"]);

                myCategory.Category_Name = tbEditCategoryName.Text;
                myCategory.Description = tbEditCategoryDescription.Text;
                myCategory.CatGroup_Id = Convert.ToInt32(ddlEditCategoryGroupList.SelectedValue);

                try
                {
                    bool editStatus = categoryDAL.EditCategory(categoryToEdit, myCategory);

                    if (editStatus)
                    {
                        HttpContext.Current.Session["editStatus"] = "Successful";
                        Response.Redirect("~/Admin/ManageCategories.aspx");
                    }
                    else
                    {
                        lblEditStatus.Text = "Unable to update category, please try again";
                        lblEditStatus.CssClass = "alert-danger";
                    }
                }
                catch (Exception ex)
                {
                    lblEditStatus.Text = Convert.ToString(ex);
                    lblEditStatus.CssClass = "alert-danger";
                }
            }
            catch (Exception ex)
            {
                updateStatus.Attributes["class"] = "alert alert-info alert-dismissable fade in";
                updateStatus.Visible = true;
                lblStatus.Text = "Invalid categoryId.";
            }
        }
        else
        {
            updateStatus.Attributes["class"] = "alert alert-info alert-dismissable fade in";
            updateStatus.Visible = true;
            lblStatus.Text = "Nothing to update.";
        }
    }

这是在我的DALayer中,其中包含与类别有关的功能。

    public bool EditCategory(int categoryToEdit, RecipeCategory newCategoryInfo)
    {
        RecipeXchangeDBContext dbContext = new RecipeXchangeDBContext();
        RecipeCategory myCategory = new RecipeCategory();
        bool status = false;

        myCategory = (from c in dbContext.RecipeCategories
                      where c.Category_Id == categoryToEdit
                      select c).First();

        myCategory.Category_Name = newCategoryInfo.Category_Name;
        myCategory.Description = newCategoryInfo.Description;
        myCategory.CatGroup_Id = newCategoryInfo.CatGroup_Id;

        try
        {
            if (dbContext.SaveChanges() == 1)
                status = true;
            else
                status = false;
        }
        catch (InvalidOperationException ex)
        {
            status = false;
        }
        return status;
    }

出于某种原因,每当我尝试使用预填充表单更新行时,代码将始终从dbContext.SaveChanges()返回0,并且不会更新数据库中的行。

注意:如果我没有填充表单,它会正常工作。

1 个答案:

答案 0 :(得分:1)

Page_Load不仅在第一次加载页面时运行,而是在每次加载页面时运行,包括用户提交表单时。结果是您在保存之前覆盖了用户的输入。

在这种情况下,由于您使用常规浏览器导航转到特定类别页面,因此您只需检查Page.IsPostBack中的Page_Load,而不是在这种情况下设置任何内容。