Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net MVC中使用EF更新特定字段_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 在asp.net MVC中使用EF更新特定字段

C# 在asp.net MVC中使用EF更新特定字段,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个应用程序,需要能够更新数据。有一个名为“主页”的表,其中包含多个字段。为了便于使用,我希望用户只更新此表中的两个字段(Description1和Description2)。在尝试实现这一点时,它会更新这两个字段,但会删除表中其他字段中的其余数据。请检查下面的代码 控制器.cs // GET: HomePages/Edit/5 public ActionResult EditDescription(int? id) { if (id

我有一个应用程序,需要能够更新数据。有一个名为“主页”的表,其中包含多个字段。为了便于使用,我希望用户只更新此表中的两个字段(Description1和Description2)。在尝试实现这一点时,它会更新这两个字段,但会删除表中其他字段中的其余数据。请检查下面的代码

控制器.cs

 // GET: HomePages/Edit/5
        public ActionResult EditDescription(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            HomePage homePage = db.HomePages.Find(id);
            if (homePage == null)
            {
                return HttpNotFound();
            }
            return View(homePage);
        }

        // POST: HomePages/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditDescription([Bind(Include = "ID,Description1,Description2,TestName1,TestName2,TestName3,TestComp1,TestComp2,TestComp3,TestDesc1,TestDesc2,TestDesc3,FooterAddress,FooterEmail,FooterTel,FooterFax")] HomePage homePage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(homePage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(homePage);
        }
cshtml

@使用(Html.BeginForm()){@Html.AntiForgeryToken()

@Html.ValidationSummary(true,“,new{@class=“text danger”})@Html.HiddenFor(model=>model.ID) @LabelFor(model=>model.Description1,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.TextAreaFor(model=>model.Description1,5100,null)@Html.ValidationMessageFor(model=>model.Description1,”,new{@class=“text danger”}) @LabelFor(model=>model.Description2,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.TextAreaFor(model=>model.Description2,8100,null)@Html.ValidationMessageFor(model=>model.Description2,”,new{@class=“text danger”})
}
对于不想编辑的字段,应使用@Html.HiddenFor标记帮助程序。这样,视图将把字段数据发送回控制器中的POST方法。例如:
@Html.HiddenFor(x=>x.TestName1)
将把TestName1字段传递给控制器中的post方法。

您正在绑定所有模型,并且只获取2个元素

[Bind(Include = "ID,Description1,Description2,TestName1,TestName2,TestName3,TestComp1,TestComp2,TestComp3,TestDesc1,TestDesc2,TestDesc3,FooterAddress,FooterEmail,FooterTel,FooterFax")]
因此,请尝试在没有绑定选项的情况下执行此操作