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