我想只更新数据库的一个字段。我在我的Controller(AOProf Controller)中创建了一个如下所示的函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Accommodation_App.Models;
using System.Data.Entity;
namespace Accommodation_App.Controllers
{
public class AOProfController : Controller
{
private StudAccommodationEntities1 SContextUser;
private DbSet<User> UserDb;
private StudAccommodationEntities1Entities SContextProp;
private DbSet<Property> Properties;
public AOProfController()
{
SContextUser = new StudAccommodationEntities1();
UserDb = SContextUser.UserDb;
SContextProp = new StudAccommodationEntities1Entities();
Properties = SContextProp.Properties;
}
[HttpPost]
public ActionResult Reject(int id)
{
Property obj = Properties.Find(id);
if (obj != null)
{
obj.isApproved = false;
UpdateModel(obj);
SContextProp.SaveChanges();
}
else{
return Content("Not such an object");
}
return Content("the rejected is confirmed");
}
在我看来,我从onclick事件中调用了这个函数。我之前发送了参数给视图。它发送正确。
<button value="Reject" onclick="location.href='@Url.Action("Reject", "AOProf", new { id = item.ProId })'" >Reject</button>
点击按钮后显示在网址栏中的网址是
http://localhost:54464/AOProf/Reject/1
看起来不错,但记录在数据库中不会更新。它将保持不变。你能帮我理解如何以正确的方式做到这一点吗?