我是编程新手并且在使用MVC 5项目时遇到了一些麻烦。具体来说,在自动生成的视图细节,编辑和删除。从另一个模型/表中提取数据的字段缺少数据。
A有一张表格,表格数据称为" Shoutouts。" Include是用户的名字和姓氏,以及一个名为" Level"其中包含带有名称和Id的加权值。
通过发现如何在ActionResult中为索引使用.Inlcude()
,我能够解决这个问题。
public ActionResult Index(string sortOrder)
{
var allShoutouts = db.Shoutouts
.Include(s => s.User)
.Include(s => s.Level);
return View(allShoutouts);
}
但我不知道如何在其他视图中做到这一点,因为他们需要ShoutoutId通过返回。
public ActionResult Details(int? id)
{
ShoutoutFormViewModel SFVM = new ShoutoutFormViewModel();
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Shoutout shoutout = db.Shoutouts.Find(id);
if (shoutout == null)
{
return HttpNotFound();
}
return View(shoutout);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Shoutout shoutout = db.Shoutouts.Find(id);
if (shoutout == null)
{
return HttpNotFound();
}
return View(shoutout);
}
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Shoutout shoutout = db.Shoutouts.Find(id);
if (shoutout == null)
{
return HttpNotFound();
}
return View(shoutout);
}
Here's what the page looks like for delete as an example of the issue.
我无法在Stack Overflow或Google上找到任何类似的问题,因此感谢任何帮助。
答案 0 :(得分:0)
因为你有id:
var shoutout = db.Shoutouts
.Include(s => s.User)
.Include(s => s.Level)
.Single(s => s.Id == id);
return View(allShoutouts);