[英]“Index was out of range” error in asp.net MVC project
我正在asp.net MVC 5項目上工作。我有兩個模型Post和Comment,它們之間存在一對多的關系。 在帖子的“詳細信息”視圖中,我想在該帖子中添加評論。 不使用任何ViewModel怎么辦? 視圖中僅傳遞Post模型。 我想使用Post模型引用訪問Comment及其屬性,畢竟它們之間有很多關系,Post模型具有Comment的引用。
這是我的代碼示例Post控制器中的Details動作
[HttpPost]
public ActionResult Details(Comment comment, string post_id)
{
int id = int.Parse(post_id);
if (ModelState.IsValid)
{
var post = db.Posts.FirstOrDefault(u => u.id == id);
comment.post = post;
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(comment);
}
//
// GET: /Post/Details/5
public ActionResult Details(int id = 0)
{
Post post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
return View(post);
}
這是Details.schtml
@model Blog.Models.Post
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<legend>Post</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.title)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.title)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.body)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.body)
</div>
@for (int i = 0; i <Model.comments.Count; i++)
{
<div class="display-label">
@Html.DisplayNameFor(model => model.comments[i].comment)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.comments[i].comment)
</div>
}
<h2>Comment</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Article</legend>
<div class="editor-label">
@Html.LabelFor(model =>model.comments[Model.comments.Count].comment)
</div>
<div class="editor-field">
@Html.EditorFor(model =>model.comments[Model.comments.Count].comment))
@Html.ValidationMessageFor(model => model.comments[Model.comments.Count].comment)
</div>
<p>
<input type="submit" value="Add" />
</p>
</fieldset>
}
</fieldset>
發生錯誤
“索引超出范圍。必須為非負數,並且小於集合的大小。\\ r \\ n參數名稱:index”
如何成功添加評論?
錯誤本身幾乎可以解釋問題。 在出現錯誤的行上, Model.comments.Count
超出范圍。 索引從0開始,因此應該以這種方式檢索最后一條注釋.... model => model.comments[Model.comments.Count - 1].comment
。
您的異常是由視圖中的以下行生成的
@Html.LabelFor(model =>model.comments[Model.comments.Count].comment)
並且還將由后續的EditorFor()
和ValidationMessageFor()
方法生成。
集合索引器從零開始,而.Count()
返回集合中的項目數。 如果說您的收藏集包含2條Comment
,則您可以按以下方式訪問它們
model.Comments[0].comment
model.Comments[1].comment
但您的[Model.comments.Count]
等於
model.Comments[2].comment
拋出異常是因為您引用的是集合中的第三個項目(不存在)。 即使您引用了集合中的現有項目,您的代碼也會失敗,因為您將回發一個集合(由索引器定義),但是您的POST方法僅適用於單個對象
似乎您想向Post
添加新評論,在這種情況下,您可以使用包含附加Comment
屬性的視圖模型
public Comment NewComment { get; set; }
然后在您查看的情況下,使用
@Html.EditorFor(m => m.NewComment.comment)
並將POST方法簽名更改為
public ActionResult Details([Bind(Prefix = "NewComment")]Comment comment, int post_id)
請注意Bind.Prefix
屬性的使用,這對於從張貼到該方法的名稱/值對中刪除"NewComment"
前綴是必需的。 還要注意,您可以將第二個參數設置為int
,避免不必要的從string
到int
轉換(盡管您似乎沒有為屬性post_id
生成表單控件,所以無論如何它將為null
)。
一種替代方法是使用@Html.Action()
調用子操作方法,該方法將基於新的Comment
返回部分視圖(包含表單)。
這是因為db.Posts.Find(id);
。 您已在public ActionResult Details(int id = 0)
為id賦予了初始值。 檢查您的代碼,以確保始終將值傳遞給id。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.