Jus*_*ier 4 c# asp.net-mvc entity-framework-4
大家好,我正在玩ASP.NET MVC和Entity Framework.我想知道什么是处理从控制器到视图再返回数据的最佳方法.我会更好地解释一下:
我有一个用于创建新"收据"对象的操作
[Authorize]
public ActionResult CreateReceipt(int id)
{
//I create the receipt object
Receipt newReceipt = new Receipt();
// assign some information about the owner of the receipt
// and the group that it belongs to
newReceipt.Group = group;
newReceipt.Owner = user;
//send off to the view to be displayed
return View(newReceipt);
}
Run Code Online (Sandbox Code Playgroud)
所以我基本上创建一个收据并预先填写一些信息(包括授权用户和一些组ID信息)然后我可以将其发送到具有各种表单元素的视图,让用户填写其他缺少的字段并提交以便添加新收据.如果收据对象中的所有字段都显示在表单上,这一切都很有效.
如果我删除用户不应该触摸的东西的表单元素(例如组号,收据所属的用户ID等等)然后当我提交表单并在控制器中提取它时:
[HttpPost]
[Authorize]
public ActionResult CreateReceipt(Receipt receipt)
{
if (ModelState.IsValid)
{
using (EntityFrameworkEntities context = new EntityFrameworkEntities)
{
context.AddToReceipts(receipt);
context.SaveChanges();
}
return RedirectToAction("Index");
}
return View(receipt);
}
Run Code Online (Sandbox Code Playgroud)
然后,我填写并发送到视图的所有方便的预加载信息都没有回复帖子.我知道我可以将UserID或GroupID放入隐藏字段,然后使用POST返回,但这感觉不对.从技术上讲,有人可以进去,更改隐藏的值并重新提交帖子.然后我可以检查以确保一切都应该在它所属的位置,但这也感觉就像是另一次访问数据库以获取我曾经拥有的信息.
如果有人能详细说明从模型到视图到控制器传递数据的标准方法,那就太棒了.感谢您的时间和帮助!