我正在使用 mv3,我正在创建应用程序,当我单击创建按钮时需要将数据提交到数据库并更新按钮下方的列表。
我也尝试过部分视图,但它显示错误:
传入字典的模型项的类型为“System.Collections.Generic.List`1[MvcStudent.stu]”,但此字典需要“MvcStudent.Models.StuModel”类型的模型项。
我在下面列出我的代码请帮助
学生控制器.cs
public class StudentController : Controller
{
//
// GET: /Student/
stdataDataContext stdb = new stdataDataContext();
public ActionResult Index()
{
return View();
}
public ActionResult create()
{
return View(stdb.stus.ToList());
}
[HttpPost]
public ActionResult create(MvcStudent.Models.StuModel stu)
{
stu student = new stu();
student.name = stu.name;
student.address = stu.addr;
stdb.stus.InsertOnSubmit(student);
stdb.SubmitChanges();
return View();
}
}
_Create.cshtml
@model MvcStudent.Models.StuModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" typ
e="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>StuModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.addr)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.addr)
@Html.ValidationMessageFor(model => model.addr)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.gen)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.gen)
@Html.ValidationMessageFor(model => model.gen)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@Html.Partial("_PartialGrid");
_PartialGrid.cshtml
@model IEnumerable<MvcStudent.Models.StuModel>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
name
</th>
<th>
addr
</th>
<th>
gen
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.addr)
</td>
<td>
@Html.DisplayFor(modelItem => item.gen)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>