我有mvc项目注册新学生并给他课程和学位等,如数据库所示(我使用实体框架)database of the project 。编辑视图代码是:
@model IList<HighStudy.Models.Grade>
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title" align="center">Student Info</h3>
</div>
<div class="panel-body">
<div class="col-lg-4">
<p>Name: @Html.DisplayFor(model => model[0].Student.Name)</p>
<p>Level: @Html.DisplayFor(model => model[0].Student.Level)</p>
</div>
<div class="col-lg-4">
<p>Department: @Html.DisplayFor(model => model[0].Student.Department)</p>
<p>Study Type: @Html.DisplayFor(model => model[0].Student.StudyType)</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-6">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title" align="center">Course 1</h3>
</div>
<div class="panel-body">
<ul class="list-group">
@for (var i = 0; i < Model.Count; i++)
{
if (Model[i].CourseNumber == 1)
{
if (Model[i].Mark == "A" || Model[i].Mark == "B")
{
<li class="list-group-item list-group-item-success">
<div class="row">
<div class="col-lg-8">
@Model[i].Cours.Course_Title
@Html.TextBoxFor(model => Model[i].Cours.Course_Title, new { style = "width:50%" })
@Html.ValidationMessageFor(model => Model[i].Cours.Course_Title)
</div>
<div class="col-lg-2">
@Model[i].Grade1
@Html.TextBoxFor(model => Model[i].Grade1, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Grade1)
</div>
<div class="col-lg-2">
@Model[i].Mark
@Html.TextBoxFor(model => Model[i].Mark, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Mark)
</div>
</div>
</li>
}
else
{
<li class="list-group-item list-group-item-danger">
<div class="row">
<div class="row">
<div class="col-lg-8">
@Model[i].Cours.Course_Title
@Html.TextBoxFor(model => Model[i].Cours.Course_Title, new { style = "width:50%" })
@Html.ValidationMessageFor(model => Model[i].Cours.Course_Title)
</div>
<div class="col-lg-2">
@Model[i].Grade1
@Html.TextBoxFor(model => Model[i].Grade1, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Grade1)
</div>
<div class="col-lg-2">
@Model[i].Mark
@Html.TextBoxFor(model => Model[i].Mark, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Mark)
</div>
</div>
</div>
</li>
}
}
}
</ul>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title" align="center">Course 2</h3>
</div>
<div class="panel-body">
<ul class="list-group">
@for (var i = 0; i < Model.Count; i++)
{
if (Model[i].CourseNumber == 2)
{
if (Model[i].Mark == "A" || Model[i].Mark == "B")
{
<li class="list-group-item list-group-item-success">
<div class="row">
<div class="col-lg-8">
@Model[i].Cours.Course_Title
@Html.TextBoxFor(model => Model[i].Cours.Course_Title, new { style = "width:50%" })
@Html.ValidationMessageFor(model => Model[i].Cours.Course_Title)
</div>
<div class="col-lg-2">
@Model[i].Grade1
@Html.TextBoxFor(model => Model[i].Grade1, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Grade1)
</div>
<div class="col-lg-2">
@Model[i].Mark
@Html.TextBoxFor(model => Model[i].Mark, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Mark)
</div>
</div>
</li>
}
else
{
<li class="list-group-item list-group-item-danger">
<div class="row">
<div class="col-lg-8">
@Model[i].Cours.Course_Title
@Html.TextBoxFor(model => Model[i].Cours.Course_Title, new { style = "width:50%" })
@Html.ValidationMessageFor(model => Model[i].Cours.Course_Title)
</div>
<div class="col-lg-2">
@Model[i].Grade1
@Html.TextBoxFor(model => Model[i].Grade1, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Grade1)
</div>
<div class="col-lg-2">
@Model[i].Mark
@Html.TextBoxFor(model => Model[i].Mark, new { style = "width:30px" })
@Html.ValidationMessageFor(model => Model[i].Mark)
</div>
</div>
</li>
}
}
}
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<input type="submit" value="Save" class="btn btn-default" />
</div>
}
<div class="col-lg-offset-0">
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
并在编辑视图的控制器代码中:
edit view code in the controller
。当它达到db.Entry(g).State = System.Data.Entity.EntityState.Modified;
它会阻挡!!
任何帮助都会有用,提前谢谢。
答案 0 :(得分:0)
我相信你应该迭代你的集合,将每个对象添加到上下文中,并将其设置为Modified。
public class MaximizedPopupWindowAction : PopupWindowAction
{
protected override Window GetWindow( INotification notification )
{
var window = base.GetWindow( notification );
window.SizeToContent = SizeToContent.Manual;
window.SourceInitialized += ( s, e ) => window.WindowState = WindowState.Maximized;
return window;
}
}
您必须在foreach声明之外foreach(grade in g)
{
db.Entry(grade).State = EntityState.Modified;
}
db.SaveChanges();
答案 1 :(得分:0)
db.Entry(g).State = System.Data.Entity.EntityState.Modified;
在上面的代码中,您从另一个对象获取g
,您需要从用于更改db
State
对象发起它
另一件事可能是list
,我认为state
的{{1}}无法完全修改。
考虑做这样的事情
List
最近经历了同样的错误,并且foreach救了我的命。不确定它是否是一种非常干净的方式。
答案 2 :(得分:0)
已解决
我目前的例外是:
解决它的步骤: