【问题标题】:Passing a parameter to the Action from view将参数传递给视图中的操作
【发布时间】:2021-09-23 05:15:54
【问题描述】:

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

    }

【问题讨论】:

  • 你好@GOKU,关于这个案例有什么更新吗?
  • 嗨@Yinqiu 我找到了解决方案并添加为评论。谢谢你的努力。接受你付出的努力的答案。

标签: javascript c# html asp.net-core model-view-controller


【解决方案1】:

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

<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>

测试结果:

【讨论】:

    【解决方案2】:

    @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);
    
        }
    

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2011-10-07
      相关资源
      最近更新 更多