【发布时间】:2015-06-24 16:41:40
【问题描述】:
我在我的 .NET 应用程序的索引页面上实现了一个保存按钮。当我单击它时,它会加载,但不会保存任何内容。我只是将索引页用于复选框。如果我取消选中并保存它,它会保存,但也会取消选中该行中的所有其他内容。当我尝试检查某些内容时,什么也没有发生。这是我正在使用的代码:
索引.cshtml
@using (Html.BeginForm("save", "drnos"))
{
<input type="submit" value="Save" />
}
我的复选框字段之一的示例:
<td>
@Html.EditorFor(modelItem => item.Soft)
@Html.ValidationMessageFor(modelItem => item.Soft)
</td>
drnosController.cs
[HttpPost]
public ActionResult save(Doctor doc)
{
System.Diagnostics.Debug.WriteLine("Save Called");
db.Entry(doc).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
这是整个 HTML 文件:
@model PagedList.IPagedList<drnosv6.Models.Doctor>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Index";
}
<h2>Doctors</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm()) //insert the search bar
{
<p>
Find by First Name, Last Name, or RVH ID: @Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
@using (Html.BeginForm("save", "drnos"))
{
<input type="submit" value="Save" />
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('tr:even').addClass('alt-row-class');
});
</script>
<p>
</p>
<p>Click on a column header to sort by that column</p>
<table>
<tr>
<th>
@Html.ActionLink("RVH ID", "Index", new { sortOrder = ViewBag.IDSortParm })
</th>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.LastSortParm })
</th>
<th>
@Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.FirstSortParm })
</th>
<th>
Middle Initial
</th>
<th>
Degree
</th>
<th>
Group
</th>
<th>
Adm Priv
</th>
<th>
@Html.ActionLink("QCPR", "Index", new { sortOrder = ViewBag.QCPRSortParm })
</th>
<th>
@Html.ActionLink("Keane", "Index", new { sortOrder = ViewBag.KeaneSortParm })
</th>
<th>
@Html.ActionLink("Orsos", "Index", new { sortOrder = ViewBag.OrsosSortParm })
</th>
<th>
@Html.ActionLink("Soft", "Index", new { sortOrder = ViewBag.SoftSortParm })
</th>
<th>
@Html.ActionLink("3M", "Index", new { sortOrder = ViewBag.threeMSortParm })
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
using (Html.BeginForm("Save", "Doctor", FormMethod.Post))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.RVH_ID_)
</td>
<td>
@Html.DisplayFor(modelItem => item.Last_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.First_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Middle_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Degree1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Group)
</td>
<td>
@Html.DisplayFor(modelItem => item.AdmPriv)
</td>
<td>
@Html.DisplayFor(modelItem => item.QCPR)
</td>
<td>
@Html.EditorFor(modelItem => item.Keane)
@Html.ValidationMessageFor(modelItem => item.Keane)
</td>
<td>
@Html.EditorFor(modelItem => item.Orsos)
@Html.ValidationMessageFor(modelItem => item.Orsos)
</td>
<td>
@Html.EditorFor(modelItem => item.Soft)
@Html.ValidationMessageFor(modelItem => item.Soft)
</td>
<td>
@Html.DisplayFor(modelItem => item.C3M)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.RVH_ID_ })
@Html.ActionLink("Details", "Details", new { id = item.RVH_ID_ })
</td>
</tr>
}
}
</table>
<p>
<input type="submit" value="Save" />
</p>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
【问题讨论】:
-
When I click it, it loads你是什么意思,Edit动作被调用了?如果确实如此,您能否验证SaveChanges是否使用断点调用?If I uncheck something你在说什么?我在您的表单中没有看到任何复选框。如果元素与问题相关,那么也包括它们。 -
您的输入字段必须在您的表单中,否则它们不会发布。
-
@Dimi 这是 HTML 文件:dropbox.com/s/4xs94u7uwahmwut/Index.cshtml?dl=0
-
我是否已在 Dropbox 下注册以查看您的 HTML? :)
-
@Dimi 我不这么认为!
标签: c# asp.net asp.net-mvc