「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.03.17
XML
カテゴリ: C#.NET


以下は、ASP.NET Web Forms および ASP.NET MVC で入力検証を行うためのサンプルコードです。


### ASP.NET Web Forms:


```aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Input Validation Example</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                ControlToValidate="TextBox1" ErrorMessage="Please enter a value" 

                ForeColor="Red"></asp:RequiredFieldValidator>

            <br />

            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

        </div>

    </form>

</body>

</html>

```


```csharp

using System;


namespace WebApplication1

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {


        }


        protected void Button1_Click(object sender, EventArgs e)

        {

            if (Page.IsValid)

            {

                // フォームが有効な場合の処理

            }

        }

    }

}

```


この例では、`RequiredFieldValidator` を使用してテキストボックスが空でないことを検証しています。ボタンがクリックされたときに、`Page.IsValid` プロパティを使用してフォームが有効であるかどうかをチェックし、有効な場合に処理を実行します。


### ASP.NET MVC:


```csharp

using System.ComponentModel.DataAnnotations;

using System.Web.Mvc;


namespace MvcApplication1.Models

{

    public class MyModel

    {

        [Required(ErrorMessage = "Please enter a value")]

        public string MyProperty { get; set; }

    }

}

```


```csharp

using MvcApplication1.Models;

using System.Web.Mvc;


namespace MvcApplication1.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }


        [HttpPost]

        public ActionResult Index(MyModel model)

        {

            if (ModelState.IsValid)

            {

                // モデルが有効な場合の処理

            }

            return View(model);

        }

    }

}

```


```html

@model MvcApplication1.Models.MyModel


<!DOCTYPE html>


<html>

<head>

    <title>Input Validation Example</title>

</head>

<body>

    @using (Html.BeginForm())

    {

        @Html.TextBoxFor(model => model.MyProperty)

        @Html.ValidationMessageFor(model => model.MyProperty)

        <br />

        <input type="submit" value="Submit" />

    }

</body>

</html>

```


この例では、`MyModel` クラスで `Required` 属性を使用してプロパティの検証を定義し、コントローラーのアクションメソッドで `ModelState.IsValid` プロパティを使用してモデルの検証をチェックしています。ビューでは、`TextBoxFor` ヘルパーメソッドを使用して入力フィールドを作成し、`ValidationMessageFor` ヘルパーメソッドを使用して検証エラーメッセージを表示しています。


これらのサンプルコードを使用すると、ASP.NET Web Forms および ASP.NET MVC で簡単に入力検証を実装できます。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.03.17 09:40:16


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: