ASP.NET MVC保存按钮加载但不起作用

时间:2015-04-17 17:30:06

标签: c# asp.net asp.net-mvc

我在索引页面上的.NET应用程序中实现了一个保存按钮。当我点击它,它加载,但不保存任何东西。我只是使用索引页面的复选框。如果我取消选中并保存它,它会保存,但也会取消选中该行中的所有其他内容。当我尝试检查某些东西时没有任何反应。这是我正在使用的代码:

Index.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 }))

2 个答案:

答案 0 :(得分:2)

您的BeginForm或ActionLink必须指向ActionResult Edit。

答案 1 :(得分:0)

提交表单时,只提交在该表单中声明的​​输入。您的表单中只有提交按钮,因此没有其他字段可以填写您的Doctor结构。您需要将保存按钮放在与所有其他字段相同的表单上。