从视图中将参数传递给 Action

Passing a parameter to the Action from view

在下面的代码中,我列出了数据库中的值。在同一页面中,通过单击任何单元格,它将在日期和文本区域中显示行数据。在这里,我想使用一个更新按钮,它还将在其中传递参数 id。 但是这里按钮没有传递参数值

这是视图

@model IEnumerable<ProjectTracker.Models.Note>

<table id="table" class="table" style="width: 80%;" border="1" cellpadding="3">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.NoteID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Date)
            </th>

            <th>
                @Html.DisplayNameFor(model => model.Notes)
            </th>

        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.NoteID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Notes)
                </td>
                <td>
                </td>
            </tr>
        }

    </tbody>
</table>
 <div>
    <input type="hidden" name="id" id="id" />
    <input class="txt" type="datetime" id="Date" style="width: 80%;" />
 </div>
<div>
    <textarea class="scrollabletextbox txtare" id="Notes" style="width: 80%; height:10%;"></textarea>
</div>
<script>
    var table = document.getElementById('table');

    for (var i = 1; i < table.rows.length; i++) {
        table.rows[i].onclick = function () {
            document.getElementById("id").value = this.cells[0].innerHTML;
            document.getElementById("Date").value = this.cells[1].innerHTML;
            document.getElementById("Notes").value = this.cells[2].innerHTML;

        };
    }
</script>
<div>
    <input type="button" value="Update" onclick="location.href='@Url.Action("NoteEdit", "project")' + '?id=' + $('#id').val()" />

</div>

这是动作

 public IActionResult NoteEdit(int? id)
    {
       //int NoteId =Convert.ToInt32(Request.Form["NoteID"]);
        if (id == null)
        {
            return RedirectToAction("Notes");

        }
        var getnotes =  _context.Notes.Find(id);        
        return View(getnotes);
    }
    [HttpPost]
    public IActionResult NoteEdit(Note note)
    {
        if (ModelState.IsValid)
        {
            _context.Update(note);
            _context.SaveChangesAsync();
            return RedirectToAction("Notes");

        }
        return View(note);

    }

在我的测试中,你的代码可以正常工作,或者你可以尝试修改你的代码

<script>
var table = document.getElementById('table');

for (var i = 1; i < table.rows.length; i++) {
    table.rows[i].onclick = function () {
        document.getElementById("id").value = this.cells[0].innerText;
        document.getElementById("Date").value = this.cells[1].innerText;
        document.getElementById("Notes").value = this.cells[2].innerText;

    };
}
</script>

测试结果:

@Yinqiu 你说的对,我的代码是对的,只是稍微改动了一下。 查看

    <table id="table" class="table" style="width: 80%;" border="1" cellpadding="3">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.NoteID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Date)
            </th>

            <th>
                @Html.DisplayNameFor(model => model.Notes)
            </th>

        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.NoteID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Date)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Notes)
                </td>
                <td>
                </td>
            </tr>
        }

    </tbody>

</table>

以上和以下代码包含在同一视图中

下面的代码在“@using (Html.BeginForm())”

<div>
<div>
    <input type="hidden" name="id" id="id" />
    <input class="txt" type="datetime" id="Date" value="@ViewBag.DATE" name="Date" style="width: 80%;" />
</div>
<div>
    <textarea class="scrollabletextbox txtare" id="Notes" name="Notes" style="width: 80%; height:30%;"></textarea>
</div>

<script>

    var table = document.getElementById('table');
    for (var i = 1; i < table.rows.length; i++) {
        table.rows[i].onclick = function () {
            document.getElementById("id").value = this.cells[0].innerHTML;
            document.getElementById("Date").value = this.cells[1].innerHTML;
            document.getElementById("Notes").value = this.cells[2].innerHTML.trim();

        };
    }

</script>
<div>
    <input type="submit" value="Update" formaction='/Project/NoteEdit' />
    <input type="submit" value="Add" formaction='/Project/Popup' />
    <input type="submit" value="Delete" formaction='/Project/NoteDelete' />
</div>

动作

 [HttpPost]
    public IActionResult NoteEdit(int? id,DateTime Date,string Notes)
    {
        var note = _context.Notes.Find(id);
        note.Date = Date;
        note.Notes = Notes;
        if (ModelState.IsValid)
        {
            _context.Update(note);
            _context.SaveChangesAsync();
            return RedirectToAction("Notes");

        }
        return View(note);

    }