Asp.net mvc 4 ModelContext+;";查询「;一无所获
我是ASP.NETMVC4的初学者 我在数据库中创建了一个带有表的模型。我的表中有两个条目 我尝试在控制器中发送一个条目,但modelcontext返回null。这是我的密码: 我的控制器:Asp.net mvc 4 ModelContext+;";查询「;一无所获,asp.net-mvc-4,Asp.net Mvc 4,我是ASP.NETMVC4的初学者 我在数据库中创建了一个带有表的模型。我的表中有两个条目 我尝试在控制器中发送一个条目,但modelcontext返回null。这是我的密码: 我的控制器: enter code herepublic class HomeController : Controller { //connection à la table private AnnonceModelContext db = new AnnonceModelContext();
enter code herepublic class HomeController : Controller
{
//connection à la table
private AnnonceModelContext db = new AnnonceModelContext();
public ActionResult Index()
{
AnnonceModel last_annonce = db.Annonces.Find(2);
if (last_annonce == null)
{
ViewBag.Message = "Pas d'annonce pour le moment";
return View();
}
else
{
return View(last_annonce);
}
}
我的看法:
@model wiki_bird.Models.AnnonceModel
<section id="annonce" class="content-wrapper">
<h1>Annonce</h1>
<p>
@ViewBag.Message
<br />
@Html.DisplayFor(model => model.titre)
</p>
</section>
@model wiki\u bird.Models.AnnonceModel
安奈斯
@查看包。留言
@DisplayFor(model=>model.titre)
last_annonce始终为空,则我的数据库中的表“annonce”不是空的
我希望你能帮助我!对不起,我英语不好,我不是英国人
问候,
sushi检查数据库是否有
Annonce
和id=2
?我已经在我的项目中截图了我的表:Annonces
中的id字段是否标记为主键?您是使用代码优先的方法还是从数据库(即edmx)创建模型?我遵循本教程创建模型和表格: