簡體   English   中英

如何在 Razor 頁面的 Asp.Net Core DevExtreme DataGrid 列中使用 asp-action?

[英]How do I use an asp-action in a Asp.Net Core DevExtreme DataGrid column in a Razor page?

我有一個 Asp.NetCore web 應用程序,它使用 C# 編寫的 EFC 構建。 這些網頁是使用 Razor 構建的。 我正在嘗試在頁面中使用 DevExpress DevExtreme DataGrid。

這是我的 model class 定義:

public class Owner
{
    [Key]
    public Guid OwnerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
}

這是我的頁面定義:

@model IEnumerable<Owner>
@{ Layout = "_DevExtremeLayout"; ViewBag.Title = "All Owners";}

@(Html.DevExtreme().DataGrid<SmartTracPoc.Models.Owner>()
    .DataSource(Model)
    .RemoteOperations(true)
    .Columns(columns => {
        columns.AddFor(m => m.FirstName);
        columns.AddFor(m => m.LastName);
        columns.AddFor(m => m.PhoneNumber);
        columns.AddFor(m => m.OwnerId).CellTemplate(@<text><form asp-action="DeleteOwner" method="post">
                    <input type="hidden" value="m => m.OwnerId" name="OwnerId" />
                    <input type="image" src="/icon/close.png" />
                </form>
                </text>);       
    })
)

以下是 controller 文件中的相關代碼片段:

public class OwnerController : Controller
{
    [HttpPost]
    public async Task<IActionResult> DeleteOwner(Guid ownerId)
    {
        try
        {
            var requestString = SmartTrocPocServerAddress + $"Owners/{ownerId.ToString()}";
            var response = await client.DeleteAsync(requestString);
            var responseString = await response.Content.ReadAsStringAsync();
        }
        catch (HttpRequestException ex)
        {
            Debug.WriteLine("OwnerController::DeleteOwner() - exception occured");
            Debug.WriteLine(ex.Message);
        }

        return RedirectToAction("Index");
    }
}

我想在列定義中使用 asp-action 來觸發 controller 中關聯的 Delete 方法。 controller 中的 DeleteOwner() 方法已成功觸發,但我的 id 參數為 0。

在使用 DevExtreme DataGrid 之前,以下頁面定義片段可以正常工作:

@foreach (var r in Model)
{
 <table>
  <tbody>
   <tr>
    <td>
     <form asp-action="DeleteOwner" method="post">
        <input type="hidden" value="@r.OwnerId" name="OwnerId" />
        <input type="image" src="/icon/close.png" />
     </form>
    </td>
   </tr>
  </tbody>
 </table>
}

如何將 OwnerId 綁定到對我的 controller 方法調用的調用?

嘗試這個:

<input type="hidden" value="<%- data.OwnerId %>" name="OwnerId" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM