私は単純なasp.netコアカミソリのウェブサイトを作成しようとしています。ビューからコントローラasp.netコアカミソリのページへのデータの受け渡し
私は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として表示と関係なく、あなたが入力し何のファイル
'Index'ページにリダイレクトし、それらのプロパティをそのページに渡したいですか? – jAC
投稿要求の後にフォームの下部に表示するために、入力にある値を使用したいと思います。 –
OK、私は私の答えを追加しました。注意:今は 'ModelState'が有効な場合はリダイレクトしています。つまり、onPostのリダイレクトが行われます。 – jAC