MVC / Entity Framework 编辑动作
MVC / Entity Framework Edit action
我不明白为什么这个结果总是返回 null。我知道数据库中存在 ID 100。我正在创建可以存储到数据库中的在线表单。我希望能够通过 ID 将它们拉回来以更新信息。
public ActionResult reviewPreevent(int? id)
{
id = 100;
if (id.HasValue)
{
using(formEntities db = new formEntities()) {
var form = (from a in db.form_preevent
select new preeventForm
{
id = a.id,
meeting = a.meeting,
date = (DateTime)a.eventDate,
location = a.location,
p1Foyer = (bool)a.p1Foyer,
p2Foyer = (bool)a.p2Foyer,
meetingRoom = (bool)a.meetingroom,
skRoom = (bool)a.skroom,
kk1 = (bool)a.kk1,
kk2 = (bool)a.kk2,
nursery = (bool)a.nursery,
Sanctuary = (bool)a.sanctuary,
kitchen = (bool)a.kitchen,
parkingLot = (bool)a.parkinglot,
mainLeaders = a.mainleaders,
helpers = a.helpers,
backup = a.backuphelps,
soundboard = (bool)a.soundboard,
soundboardtech = a.soundboardtech,
projector = (bool)a.projector,
projectorOp = a.projectorop,
camera = (bool)a.camera,
cameraops = a.cameraops,
livestream = (bool)a.livestream,
ushers = (bool)a.ushers,
totalUshers = (int)a.totalushers,
greeters = (bool)a.greeters,
totalGreeters = (int)a.totalgreeters,
security = (bool)a.security,
setupTime = (DateTime)a.setuptime,
setup = a.setup,
breakdown = a.breakdown,
foodItems = a.fooditems,
groceryShoppers = a.groceryshoppers,
foodPrepPersonal = a.foodprep,
estExpense = (float)a.estexpense,
estIncome = (float)a.estincome,
expense = (float)a.expense,
income = (float) a.income
}).Where(t => t.id == id).FirstOrDefault();
return View();
}
}else
{
TempData["notice"] = "No form with ID: " + id + " was found.";
return View();
}
}
还有更简单的方法可以将 sql class 与视图模型 class 匹配吗?
你们太亲密了。当您 return 向客户端显示视图时,您必须 return 表单变量。
public ActionResult reviewPreevent(int? id)
{
id = 100;
if (id.HasValue)
{
using(formEntities db = new formEntities()) {
var form = (from a in db.form_preevent
select new preeventForm
{
id = a.id,
meeting = a.meeting,
date = (DateTime)a.eventDate,
location = a.location,
p1Foyer = (bool)a.p1Foyer,
.
.
.
income = (float) a.income
}).Where(t => t.id == id).FirstOrDefault();
return View(form); //THIS LINE MODIFIED
}
}else
{
TempData["notice"] = "No form with ID: " + id + " was found.";
return View();
}
}
我不明白为什么这个结果总是返回 null。我知道数据库中存在 ID 100。我正在创建可以存储到数据库中的在线表单。我希望能够通过 ID 将它们拉回来以更新信息。
public ActionResult reviewPreevent(int? id)
{
id = 100;
if (id.HasValue)
{
using(formEntities db = new formEntities()) {
var form = (from a in db.form_preevent
select new preeventForm
{
id = a.id,
meeting = a.meeting,
date = (DateTime)a.eventDate,
location = a.location,
p1Foyer = (bool)a.p1Foyer,
p2Foyer = (bool)a.p2Foyer,
meetingRoom = (bool)a.meetingroom,
skRoom = (bool)a.skroom,
kk1 = (bool)a.kk1,
kk2 = (bool)a.kk2,
nursery = (bool)a.nursery,
Sanctuary = (bool)a.sanctuary,
kitchen = (bool)a.kitchen,
parkingLot = (bool)a.parkinglot,
mainLeaders = a.mainleaders,
helpers = a.helpers,
backup = a.backuphelps,
soundboard = (bool)a.soundboard,
soundboardtech = a.soundboardtech,
projector = (bool)a.projector,
projectorOp = a.projectorop,
camera = (bool)a.camera,
cameraops = a.cameraops,
livestream = (bool)a.livestream,
ushers = (bool)a.ushers,
totalUshers = (int)a.totalushers,
greeters = (bool)a.greeters,
totalGreeters = (int)a.totalgreeters,
security = (bool)a.security,
setupTime = (DateTime)a.setuptime,
setup = a.setup,
breakdown = a.breakdown,
foodItems = a.fooditems,
groceryShoppers = a.groceryshoppers,
foodPrepPersonal = a.foodprep,
estExpense = (float)a.estexpense,
estIncome = (float)a.estincome,
expense = (float)a.expense,
income = (float) a.income
}).Where(t => t.id == id).FirstOrDefault();
return View();
}
}else
{
TempData["notice"] = "No form with ID: " + id + " was found.";
return View();
}
}
还有更简单的方法可以将 sql class 与视图模型 class 匹配吗?
你们太亲密了。当您 return 向客户端显示视图时,您必须 return 表单变量。
public ActionResult reviewPreevent(int? id)
{
id = 100;
if (id.HasValue)
{
using(formEntities db = new formEntities()) {
var form = (from a in db.form_preevent
select new preeventForm
{
id = a.id,
meeting = a.meeting,
date = (DateTime)a.eventDate,
location = a.location,
p1Foyer = (bool)a.p1Foyer,
.
.
.
income = (float) a.income
}).Where(t => t.id == id).FirstOrDefault();
return View(form); //THIS LINE MODIFIED
}
}else
{
TempData["notice"] = "No form with ID: " + id + " was found.";
return View();
}
}