我有asp.net MVC应用程序,可以从数据库中检索数据。它看起来像这样:
如果我的网址是这样的:
myurl.com/Home/Index/1
我的观点是这样的:[https:// scontent-b-fra.xx.fbcdn.net/hphotos-xfp1/t31.0-8/10571949_782177481813619_1929089026797570629_o.jpg][1]
如果url是这样的: -
myurl.com/Home/Index/2
等。
我正在传递并在url中,这是我数据库中数据的ID: -
public ActionResult Index(int id)
{
BIOEntities db = new BIOEntities();
AddBio bio = new AddBio();
Biography biography = db.Biographies.Where(x => x.Id == id).FirstOrDefault();
bio.Id = biography.Id;
bio.Photo = biography.Photo;
bio.Name = biography.Name;
bio.Position = biography.Position;
return View(bio);
}
现在我想要,如果我的网址
myurl.com/Home/Index
任何人都请解释我怎么能这样做
[1]: https://%20scontent-b-fra.xx.fbcdn.net/hphotos-xfp1/t31.0-8/10571949_782177481813619_1929089026797570629_o.jpg
并且有我的观点(Customhelper用于图片..):
@model BioMVC.Models.AddBio
@using BioMVC.CustomHtmlHelpers
@{
ViewBag.Title = "Index";
Layout = "~/Views/_layout.cshtml";
}
<div id="first">
<div class="display-field">
@Html.Image(@Model.Photo, new { @id = "FirstPhoto" })
</div>
<legend class="orangeText">@Model.Name</legend>
<legend class="orangeText">@Model.Position</legend>
</div>
<div id="second">
<div class="display-field">
@Html.Image(@Model.Photo, new { @id = "secondPhoto" })
</div>
<legend class="orangeText">@Model.Name</legend>
<legend class="orangeText">@Model.Position</legend>
</div>
<div id="third">
<div class="display-field">
@Html.Image(@Model.Photo, new { @id = "thirdPhoto" })
</div>
<legend class="orangeText">@Model.Name</legend>
<legend class="orangeText">@Model.Position</legend>
</div>
<div id="fourth">
<div class="display-field">
@Html.Image(@Model.Photo, new { @id = "fourthPhoto" })
</div>
<legend class="orangeText">@Model.Name</legend>
<legend class="orangeText">@Model.Position</legend>
</div>
答案 0 :(得分:0)
乔治,
没有看到您当前的视图代码,我假设它是以下内容:
@model yournamespace.models.AddBio
因此,只会根据当前索引操作方法中的代码显示SINGLE实例。您需要做的是使该模型成为List<AddBio>
:
@model List<yournamespace.models.AddBio>
然后,您可以围绕列表进行迭代,以便像以前一样进行填充。您还需要更改索引方法以引用List&lt;&gt;而不是单个实体(提示:删除第3行上的.FirstOrDefault()
是您的起点 - 其余的只是传递中的常识返回列表&lt;&gt;而不是单个实例。)
希望这会有所帮助..
[edit] - 根据stephen muecke的回复,您还应该测试可以为空的id -good点!!