2012-01-28 10 views
-1

を「フォームを作成」で年齢を表示します。自動的に私は自分のフォームを作成中に誕生日と年齢のフィールドを持っていると私は誕生日が記入された後、自動的に年齢を計算し、表示したい私の

だから私はEntityFrameworkのCodeFirstを使用そして、MVC 3かみそりビュー

だからここに私のモデルです:

namespace Payroll_System.Models 
    { 
    public class Employee 
    { 
    [DataType(DataType.Date)] 
    [DisplayName("Date of Birth:")] 
    [Required(ErrorMessage = "Birth Date is Required.")] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0;dd/MM/yyyy}" 
    , NullDisplayText = "No Date of Birth is Selected.")] 
    public DateTime BirthDate { get; set; } 


    [Integer] 
    [Min(1, ErrorMessage = "Unless you are Benjamin Button.")] 
    public int Age 
    { 
     get 
     { 
      DateTime now = DateTime.Today; 
      int age = now.Year - BirthDate.Year; 
      if (BirthDate > now.AddYears(-age)) age--; ; 
      return age; 
     } 

    } 

そして私は、私はMVC足場を使用してCRUDオプションを使用して私のコントローラを作成したモデルを作成した後。 Createの部分ビューには、年齢のテキストボックスはありません。だから、いくつかのコードを提供してください。

+0

次のコードである必要があり、時間を保存する場合、これは間違っていますか?あなたはおそらく表示ビューでそれを見つけるでしょう、作成や編集ではありません。また、 '[Integer]'と '[Min]'属性は何ですか?また、検証のポイントは読み取り専用プロパティとは何ですか? – Kobi

答えて

0

値を入力することはできません。は読み取り専用プロパティです。

IValidatableObjectインターフェイスを実装してデータを検証することができます。

モデル:

public class PersonFromSO : IValidatableObject 
{ 
    [DataType(DataType.Date)] 
    [DisplayName("Date of Birth:")] 
    [Required(ErrorMessage = "Birth Date is Required.")] 
    [DisplayFormat(ApplyFormatInEditMode = true, 
        DataFormatString = "{0;dd/MM/yyyy}", 
        NullDisplayText = "No Date of Birth is Selected.")] 
    public DateTime BirthDate { get; set; } 

    public int Age 
    { 
     get 
     { 
      DateTime now = DateTime.Today; 
      int age = now.Year - BirthDate.Year; 
      if (BirthDate > now.AddYears(-age)) age--; ; 
      return age; 
     } 

    } 

    public IEnumerable<ValidationResult> Validate(ValidationContext context) 
    { 
     if (Age < 1) 
      yield return new ValidationResult("You were born in the future.", 
               new[] { "Age" }); 
    } 
} 

ビュー:必要に応じて

@model PersonFromSO 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) 
{ 

    @Html.ValidationSummary() 

    @Html.LabelFor(m => m.BirthDate) 
    @Html.TextBoxFor(m => m.BirthDate) 

    <time>@Model.Age</time> 

    <input type="submit" /> 
} 

は、ここで私が使用したコントローラは、またです:

public class PersonFromSOController : Controller 
{ 
    public ActionResult Create() 
    { 
     return View(new PersonFromSO()); 
    } 

    [HttpPost] 
    public ActionResult Create(PersonFromSO person) 
    { 
     return View(person); 
    } 
} 

私は少し複雑であることに注意しますあなたはおそらくBirthDateの妥当性を検証する方が良いでしょう。

+0

これは私の最初のMVCの答えです - 私はOKだったと思います。 – Kobi

-1
get 
{ 
    DateTime now = DateTime.Today; 
    int age = now.Year - BirthDate.Year; 
    if (BirthDate > now.AddYears(-age)) age--; ; 
    return age; 
} 

あなたも、あなたが*ユーザーは*読み取り専用のプロパティに彼女の年齢を入力することを期待することができますどのように

get 
{ 
    DateTime now = DateTime.Now; 
    int age = now.Year - BirthDate.Year; 
    if (BirthDate > now.AddYears(-age)) age--; ; 
    return age; 
} 
関連する問題