ASP.NET MVC序列不包含任何元素 [英] ASP.NET MVC Sequence contains no elements
问题描述
我的HomeController中有以下代码:
I have the following code in my HomeController:
public ActionResult Edit(int id)
{
var ArticleToEdit = (from m in _db.ArticleSet where m.storyId == id select m).First();
return View(ArticleToEdit);
}
[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Article ArticleToEdit)
{
var originalArticle = (from m in _db.ArticleSet where m.storyId == ArticleToEdit.storyId select m).First();
if (!ModelState.IsValid)
return View(originalArticle);
_db.ApplyPropertyChanges(originalArticle.EntityKey.EntitySetName, ArticleToEdit);
_db.SaveChanges();
return RedirectToAction("Index");
}
这是Edit方法的视图:
And this is the view for the Edit method:
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="headline">Headline</label>
<%= Html.TextBox("headline") %>
</p>
<p>
<label for="story">Story <span>( HTML Allowed )</span></label>
<%= Html.TextArea("story") %>
</p>
<p>
<label for="image">Image URL</label>
<%= Html.TextBox("image") %>
</p>
<p>
<input type="submit" value="Post" />
</p>
</fieldset>
<% } %>
当我按下提交"按钮时,我得到了一个错误:序列在此行上没有元素:var originalArticle = (from m in _db.ArticleSet where m.storyId == ArticleToEdit.storyId select m).First();
When I hit the submit button I get the error: Sequence contains no elements on this line: var originalArticle = (from m in _db.ArticleSet where m.storyId == ArticleToEdit.storyId select m).First();
出什么问题了?我如何解决它.谢谢
What is the problem? How do I fix it. Thanks
推荐答案
您需要确定的是,在编辑记录时,需要告诉数据库要编辑的记录.除非您明确告知模型使用该ID(请参阅我的第二个选项),否则在查询字符串中具有ID是不够的,但是最简单的方法是在表单中添加字段.
You need to be sure of is that when you're editing a record, you need to tell the database what record to edit. It's not enough to have the ID in the querystring unless you specifically tell the Model to use that ID (see my second option), however the easiest way to do that is add the field in your form.
<% using (Html.BeginForm()) {%>
<%= Html.HiddenFor("storyId") %>
<fieldset>
<legend>Fields</legend>
<p>
<label for="headline">Headline</label>
<%= Html.TextBox("headline") %>
</p>
<p>
<label for="story">Story <span>( HTML Allowed )</span></label>
<%= Html.TextArea("story") %>
</p>
<p>
<label for="image">Image URL</label>
<%= Html.TextBox("image") %>
</p>
<p>
<input type="submit" value="Post" />
</p>
</fieldset>
<% } %>
第二个选项显示了如何在事实之后指定故事ID,方法是通过查询字符串将其提交给操作.您只需要确保表单操作包含?storyId=[n]
This second option shows how to specify the storyId after the fact by submitting it to the Action via the Querystring. You just have to make sure the form action includes ?storyId=[n]
[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Article ArticleToEdit, int storyId)
{
ArticleToEdit.storyId = storyId;
if (ModelState.IsValid) {
_db.ApplyPropertyChanges(originalArticle.EntityKey.EntitySetName, ArticleToEdit);
_db.SaveChanges();
return RedirectToAction("Index");
} else {
return View(ArticleToEdit);
}
}
I would also suggest using something like AutoMapper to map ArticleToEdit
to ArticleSet
so that you don't need to make an additional DB lookup just to grab the original article. Basically if you are able to map ArticleToEdit
to the ArticleSet
Model, then you can use LINQ to SQL to perform the update without first querying for the storyId.
这篇关于ASP.NET MVC序列不包含任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!