从视图传递数据到控制器的asp.net core razor页面

3

我将尝试创建一个简单的asp.net core razor网站。

我有一个cshtml页面:

@page

@using RazorPages

@model IndexModel

@using (Html.BeginForm()) {
  <label for="age">How old are you?</label>
  <input type="text" asp-for="age">
  <br/>
  <label for="money">How much money do you have in your pocket?</label>
  <input type="text" asp-for="money">
  <br/>
  <input type="submit" id="Submit">
}

和一个 cs 文件:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Threading.Tasks;

namespace RazorPages
{
  public class IndexModel : PageModel
  {
    protected string money { get; set; }
    protected string age { get; set; }
    public IActionResult OnPost()
    {
      if (!ModelState.IsValid)
      {
        return Page();
      }



      return RedirectToPage("Index");

    }
  }
}

我希望能够将年龄和金钱传递给cs文件,然后通过get请求将其传回到cshtml文件,在提交按钮之后显示在页面上。我该如何实现?

更新: 以下代码无法运行。 index.cshtml.cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Threading.Tasks;



namespace RazorPages
{
  public class IndexModel : PageModel
  {
    [BindProperty]
    public decimal Money { get; set; }
    [BindProperty]
    public int Age { get; set; }
    public IActionResult OnPost()
    {
 /*     if (!ModelState.IsValid)
      {
        return Page();
      }*/
    this.Money = Money;
    this.Age = Age;







 System.IO.File.WriteAllText(@"C:\Users\Administrator\Desktop\murach\exercises\WriteText.txt", 
this.Money.ToString());
return RedirectToPage("Index", new { age = this.Age, money = this.Money});

    }
  }
}

并且在index.cshtml中:

 @page
    @using RazorPages


    @model IndexModel

    @using (Html.BeginForm()) {
      <label for="Age">How old are you?</label>
      <input type="text" asp-for="Age">
      <br/>
      <label for="Money">How much money do you have in your pocket?</label>
      <input type="text" asp-for="Money">
      <br/>
      <input type="submit" id="Submit">


    }
    Money: @Model.Money
    Age: @Model.Age

无论您输入什么,页面和文件上的“金额”和“年龄”都显示为0。


我想使用输入框中的值,在提交请求后在表单底部显示。 - user6781613
1
不是100%确定,但是我在@jAC的回答基础上添加了一些内容:将Bindable属性中的protected改为public。 - Bart Calixto
1
@BartCalixto:确实,否则它将无法从“Page”访问,抛出“'Page.Age' 由于其保护级别而无法访问”的异常。 - jAC
发布的解决方案对我无效。请参见问题中的更新部分。 - user6781613
你现在把几件事情混在一起了,我会更新我的答案,使其更加清晰。 - jAC
显示剩余2条评论
1个回答

7
在你的.cshtml文件中添加代码,以输出通过POST填充的值。

MyPage.cshtml

@page
@model IndexModel  
@using (Html.BeginForm())
{
    <label for="Age">How old are you?</label>
    <input type="text" asp-for="Age">
    <br />
    <label for="Money">How much money do you have in your pocket?</label>
    <input type="text" asp-for="Money">
    <br />
    <input type="submit" id="Submit">  
}
Money: @Model.Money
Age: @Model.Age

现在在您的模型中的每个属性上添加[BindProperty],这样您就可以从OnPost()更新它们:
[BindProperty]
public int Age { get; set; }
[BindProperty]
public decimal Money { get; set; }

此外,正如Bart Calixto所指出的那样,这些属性必须是公共的才能从你的Page中访问。
由于ASP.NET Core通过[BindProperty]进行绑定,因此OnPost()方法非常简单。
public IActionResult OnPost()
{
    return Page();
}

现在,您可以点击 提交 按钮,然后页面应该看起来像这样:

Razor Page Post

顺便提一下:属性名称需要以大写字母开头


1
作为一个小提示:如果您想将此信息重定向到另一页,可以使用return RedirectToPage("SomeOtherPage", new { age = this.Age, money = this.Money}); - jAC
我正在使用一个空的网站模板。你的例子是使用 Razor 页面模板。 - user6781613
2
你的示例也使用了 Razor Pages。你在帖子中标记了 [tag:razor-pages],你的标题包含 razor pages,而你的代码是一个 razor page (@page)。你所说的空网站模板是什么意思? - jAC
我的代码缺少renderbody()函数以及layout.cshtml、viewimports等文件... - user6781613

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接