我在按ActionLink上的编辑(视图的最后一行)时尝试从数据库发送数据。在视图控制器中遇到一些问题,以了解如何将此数据发送到正确的输入字段,以便我可以编辑它并将其发送回数据库。
(此问题仅适用于GET部分,因此我没有包含Post控制器)
@model IEnumerable<CadProject.Models.CourseModel>
@using (Html.BeginForm("AddCourse", "AdminModel", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.Label("Emnekode", new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input name="CourseCode" type="text" />
</div>
</div>
<div class="form-group">
@Html.Label("Emne", new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input name="CourseName" type="text" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Upload" class="btn btn-default" />
</div>
</div>
</div>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseCode)
</th>
<th>
@Html.DisplayNameFor(model => model.CourseName)
</th>
<th>
Edit/Delete
</th>
</tr>
@if (Model != null)
{
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CourseCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.CourseName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
}
</table>
public ActionResult Edit(int id)
{
db.Courses.Find(id);
CourseModel model = db.Courses.Find(id);
if(model == null)
{
return View();
}
return View("AddCourse", db.Courses);
}
public class CourseModel
{
[Key]
public int Id { get; set; }
public string CourseCode { get; set; }
public string CourseName { get; set; }
}
答案 0 :(得分:0)
Edit(HttpGet)方法的功能是获取用户想要编辑的项目的ID,使用此ID在数据库中查找 ONE 记录并发送找到的记录在编辑视图中,您有各种控件显示当前值,并允许用户通过使用文本框等更改它们
在您发布的代码中,您查找用户想要编辑的课程,然后将所有课程返回到编辑视图,这是错误的。它应该是这样的:
public ActionResult Edit(int id) { db.Courses.Find(ID);
CourseModel model = db.Courses.Find(id);
if(model == null)
{
return View();
}
return View("EditCourse", model);
}
看看这个简单的例子:
修改操作:
[HttpGet]
public ActionResult Edit(int id)
{
var course1 = new Course { ID = 0, Name = "Software Engineering" };
var course2 = new Course { ID = 1, Name = "ASP.NET MVC" };
var course3 = new Course { ID = 2, Name = "C# Course" };
var courses = new List<Course> { course1 ,course2, course3};
var course = courses.FirstOrDefault(c => c.ID == id);
return View("EditCourse",course);
}
编辑课程查看:
@model Example.Models.Course
@{
ViewBag.Title = "EditCourse";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Course</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}