2017-08-24 32 views
2

私は単純な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として表示と関係なく、あなたが入力し何のファイル

+0

'Index'ページにリダイレクトし、それらのプロパティをそのページに渡したいですか? – jAC

+0

投稿要求の後にフォームの下部に表示するために、入力にある値を使用したいと思います。 –

+0

OK、私は私の答えを追加しました。注意:今は 'ModelState'が有効な場合はリダイレクトしています。つまり、onPostのリダイレクトが行われます。 – jAC

答えて

3

追加POSTを介して入力した値を出力するコードを含む.cshtmlファイル。さらに、

[BindProperty] 
public int Age { get; set; } 
[BindProperty] 
public decimal Money { get; set; } 

Bart Calixtoが指摘したように、それらを:

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 

今、あなたはあなたのOnPost()から更新するモデル、の各プロパティに[BindProperty]を追加Pageからアクセスできるようにするには、プロパティを公開する必要があります。

OnPost()メソッドは、ASP.NETコアがバックグラウンドで([BindProperty]経由のバインディングのおかげで)すべての作業を行っているので、とても簡単です。あなたはSubmitと出来上がりをクリックすることができます

public IActionResult OnPost() 
{ 
    return Page(); 
} 

だから今、ページは次のようになります。

Razor Page Post

ところで:プロパティはcapital letter in the beginningで書かれています。

+1

小さな情報として: 'RedirectToPage(" SomeOtherPage "、new {age = this.Age、money = this。お金}}; 'この情報を**別の**ページにリダイレクトしたい場合に備えています。 – jAC

+0

空のWebサイトテンプレートを使用しています。あなたの例は、かみそりのページテンプレートを使用しています –

+1

あなたの例はまた、かみそりページを使用しています。あなたは[tag:razor-pages]でこの投稿にタグを付けました。あなたのタイトルにはかみそりのページが含まれていて、コードはかみそりページ( '@ page')です。空のWebサイトテンプレートはどういう意味ですか? – jAC

関連する問題