如何使用ASP.NET Core的Anchor标签助手传递整个模型?

huangapple go评论57阅读模式
英文:

How do I pass the entire model using ASP.NET Core's Anchor Tag Helper?

问题

如何在 [HttpPost] 方法中使用 Anchor Tag 传递模型?

我尝试使用 <input asp-for="@model">

<div class="list-group">
    @foreach (var item in Model) {
        <a asp-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>

参考链接:https://stackoverflow.com/a/46773721/22019322

但这会调用控制器中的 [HttpGet] 方法:

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

// POST: PostModels/Create
// 为防止绑定攻击,请启用要绑定到的特定属性。
// 更多详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
[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);
}

如何将整个 Model 传递给 Anchor Tag

编辑添加 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; }
}

当我打开一个帖子以进行编辑时,我想将整个模型传递给控制器以保存,而不是单独传递模型的每个属性。

英文:

How do I pass the model using the Anchor Tag for a [HttpPost] method?

I tried using the <input asp-for="@model">:

<div class="list-group">
    @foreach (var item in Model) {
        <a asp-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>

Ref: https://stackoverflow.com/a/46773721/22019322

But that calls the [HttpGet] method in my controller:

// 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.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[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);
}

How do I pass the entire Model in the Anchor Tag?

EDIT add 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; }
}

When I have a Post open to edit, I want to pass the whole model to the controller to save instead of passing each property of the model individually.

答案1

得分: 1

感谢提供模型详情。

我没有理解你的需求背后的逻辑。让我解释一下你遇到的问题。

首先,你在 foreach 中使用了一个链接和一个输入字段。这只会为链接和输入创建一个 HTML,其中的项目对象名称为文本。
你可以通过浏览器的 "查看页面源代码" 选项来检查所创建的 HTML 结果。

当你点击链接时,它不会提交任何内容。它只会发起一个新的 GET 请求。因此,你会进入 GET 处理程序。(如果你的数据以查询字符串的形式存在,你仍然可以通过将它们定义为方法参数从 GET 处理程序中获取它们。或者,如果查询字符串中的名称与你的模型属性匹配,模型绑定器可能会将它们绑定。)

其次,要将一些数据从视图传递到控制器进行 POST,你需要创建一个表单并提交表单元素的值。例如,如果你有一个名为 "somevalue" 的文本输入字段,这个值可以绑定到你的 POST 方法,并且可以作为字符串值读取。

你可以这样提交值:

<form id="sampleform" method="post">
    <input type="text" name="samplevalue" />
    <input type="submit" name="submitbutton" value="Submit" />
</form>

你可以这样获取值:

public void OnPost(string samplevalue)
{

}

你可以将这个逻辑应用到你的代码中。

另外,你可以查看这个完整的工作示例。
https://github.com/okutbay/MongoHead/blob/master/MongoHeadSample/Pages/People/Form.cshtml.cs

这是一个可用的示例:
http://mongohead.okb.io/people

我建议你查看这个网站。它包含了你所需要的一切。
https://www.learnrazorpages.com/

英文:

Thanks for the model details.

I did not get the logic that behind your needs. Let me explain problems that you are having.

First you are using a link and an input field in foreach. This will just create a html for link and input with item object name text.
You may check the created html result from your browser with "view page source" option.

When you click the link, it will not post anything. You will just initiate a new get request. For that reason, you are falling in to the get handler. (If have your data as query string you may still get them from the get handler by defining them as method parameters. Of if the names in query string matches to your model properties model binder may bind them.)

Second, to pass some data on post from view to controller, you need to create a form and submit values which are form elements of the form. For instance, if you have a input type text with the name of "somevalue" this value can be bind to your post method and can be read as string value.

You may post value like this:

&lt;form id=&quot;sampleform&quot; method=&quot;post&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;samplevalue&quot; /&gt;
    &lt;input type=&quot;submit&quot; name=&quot;submitbutton&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;

And you may get the value like this:

public void OnPost(string samplevalue)
{

}

You may apply this logic to your code.

Also, you may check this full working sample.
https://github.com/okutbay/MongoHead/blob/master/MongoHeadSample/Pages/People/Form.cshtml.cs

Here is the working sample:
http://mongohead.okb.io/people

I advise you to check this site. It has all you need.
https://www.learnrazorpages.com/

huangapple
  • 本文由 发表于 2023年6月22日 19:05:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定