繁体   English   中英

从视图中将参数传递给 Action

[英]Passing a parameter to the Action from view

在下面的代码中,我列出了 database 中的值。 在同一页面中,通过单击任何单元格,它将在日期和文本区域中显示行数据。 在这里我想使用一个更新按钮,它也将传递参数 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);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM