You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ASP.NET Core的Anchor Tag Helper传递完整模型至HttpPost方法?

如何通过Anchor Tag将模型传递给[HttpPost]方法?

我尝试使用<input asp-for="@model">来传递模型,代码如下:

<div class="list-group">
    @foreach (var item in Model) {
        <a asp-controller="PostModels" asp-action="Create" class="list-group-item list-group-item-action" id="@item.ID">@item.Title @item.EventDate</a><br />
        <span>@item.Line1</span>
        <input asp-for="@item" />
    }
</div>

但这样调用的是控制器中的[HttpGet]方法,控制器代码如下:

// GET: PostModels/Create
public IActionResult Create()
{
    var model = new PostModel();
    return View(model);
}

// POST: PostModels/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,Title,Posted,EventDate,Line1,Image1,Line2,Image2,Line3,Image3")] PostModel postModel)
{
    if (ModelState.IsValid)
    {
        db.Add(postModel);
        await db.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(postModel);
}

如何通过Anchor Tag传递完整的Model?

编辑补充PostModel:

public class PostModel
{
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime Posted { get; set; }
    public DateTime EventDate { get; set; }
    public string Line1 { get; set; }
    public string Image1 { get; set; }
    public string Line2 { get; set; }
    public string Image2 { get; set; }
    public string Line3 { get; set; }
    public string Image3 { get; set; }
    public Guid ResetToken { get; set; }
}

我希望打开帖子编辑时,能把整个模型传递给控制器保存,而不是逐个传递模型的属性。


内容的提问来源于stack exchange,提问作者user22019322

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 07:15:40