C# 预填充表单导致DbContext.SaveChanges()出现问题

C# 预填充表单导致DbContext.SaveChanges()出现问题,c#,asp.net,entity-framework,linq,C#,Asp.net,Entity Framework,Linq,在进行了大量调试之后,我缩小了我的dbContext更新无法工作的范围 我有一个在Page\u Load上运行的脚本,它将基于查询字符串category\u Id填充表单,该字符串是我的表的主键 protected void Page_Load(object sender, EventArgs e) { // Populate Edit Fields if (Request.QueryString["categoryId"] != null) {

在进行了大量调试之后,我缩小了我的
dbContext
更新无法工作的范围

我有一个在
Page\u Load
上运行的脚本,它将基于查询字符串
category\u 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_按钮时运行,它应该更新数据库中的行并重定向到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,并且不会更新数据库中的行


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

Page\u Load
不仅在第一次加载页面时运行,它还会在每次加载页面时运行,包括用户提交表单时。结果是,您在保存用户输入之前覆盖了它


在这种情况下,由于您使用常规浏览器导航进入特定类别页面,因此您只需在
page\u Load
中选中
page.IsPostBack
,而不必在这种情况下进行任何设置。

您不需要填充page\u Load中的字段。ASP.NET viewstate在回发期间维护控件的值。您需要这样做有什么具体原因吗?@ChetanRanpariya Err,因为在此之前我有一个页面,显示所有类别,并带有一个编辑按钮,该按钮将其链接到此页面,url中的类别id作为查询字符串。在此页面上,它将使用category_id填充表单,以便于用户编辑。