Created
January 9, 2012 14:21
-
-
Save ishisaka/1583138 to your computer and use it in GitHub Desktop.
バリデーション後の表示切り替えのための条件判断で悩む。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
Layout = "~/_Layout.cshtml"; | |
var title = Request["Title"]; | |
var titleError = false; | |
var summary = Request["Summary"]; | |
var summaryError = false; | |
var body = Request["Body"]; | |
var bodyError = false; | |
if (IsPost) { | |
if (title.IsEmpty()) { | |
titleError = true; | |
ModelState.AddError("Title", "記事のタイトルがありません。"); // | |
} | |
if (summary.IsEmpty()) { | |
summaryError = true; | |
ModelState.AddError("Summary", "要約の記入が必要です。"); | |
} | |
if (body.IsEmpty()) { | |
bodyError = true; | |
ModelState.AddError("Body", "本文の記入が必要です。"); | |
} | |
} | |
} | |
@section Header{ | |
<h1>ブログ記事の追加</h1> | |
} | |
@if (IsPost && ModelState.IsValid) { | |
Database.Open("html5practice") | |
.Execute("INSERT INTO Posts(Title, Summary, Body, PublicationDate) " | |
+ "VALUES(@0, @1, @2, @3)", title, summary, body, DateTime.Now); | |
<form> | |
<fieldset> | |
<legend>新しいブログ記事の送信</legend> | |
<div class="clearfix"> | |
<label for="Titel">題名:</label> | |
<div class="input"> | |
<span class="uneditable-input">@title</span> | |
</div> | |
</div> | |
<div class="clearfix"> | |
<label for="summary">要約:</label> | |
<div class="input"> | |
<span class="uneditable-input">@summary</span> | |
</div> | |
</div> | |
<div class="clearfix"> | |
<label for="Body">本文:</label> | |
<div class="input"> | |
<p class="uneditable-input">@body</p> | |
</div> | |
</div> | |
</fieldset> | |
</form> | |
} | |
else { | |
<form method="post" action=""> | |
<fieldset> | |
<legend>新しいブログ記事の送信</legend> | |
@* | |
エラーサマリーを表示する場合にはこうする。 | |
@Html.ValidationSummary() | |
*@ | |
@* | |
どっちがスマートかな? | |
方法1:エラーを確認するメソッドを使用してエラーの有無を判断する | |
*@ | |
<div class="@(BootStrapHelper.IsError(ModelState, "Title") ? "clearfix error" : "clearfix")"> | |
<label for="Titel">題名:</label> | |
<div class="input"> | |
<input type="text" name="Title" value="@Request["Title"]"/> | |
<span class="help-inline">@Html.ValidationMessage("Title", "* タイトルの入力が必要です")</span> | |
</div> | |
</div> | |
@* | |
方法2:エラーを示すフラグを使用して判断する。 | |
*@ | |
<div class="@(summaryError ? "clearfix error" : "clearfix")"> | |
<label for="summary">要約:</label> | |
<div class="input"> | |
<input type="text" name="Summary" value="@Request["Summary"]"/> | |
<span class="help-inline">@Html.ValidationMessage("Summary", "* 要約の入力が必要です")</span> | |
</div> | |
</div> | |
<div class="@(bodyError ? "clearfix error" : "clearfix")"> | |
<label for="Body">本文:</label> | |
<div class="input"> | |
<textarea cols="40" rows="10" name="Body"> | |
</textarea> | |
<span class="help-inline">@Html.ValidationMessage("Body", "* 本文の入力が必要です")</span> | |
</div> | |
</div> | |
<div class="actions"> | |
<button type="submit" class="btn primary">ポスト</button> | |
</div> | |
</fieldset> | |
</form> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@functions { | |
/// <summary> | |
/// 引数modelで与えられたキーでModelStateDictionaryにエラーが登録されていたらTrueを返す。 | |
/// </summary> | |
/// <param name="dic">呼び出し元のページのModelStateプロパティを渡す</param> | |
/// <param name="model">対象とするmodelを示すキー名</param> | |
/// <returns></returns> | |
public static bool IsError(ModelStateDictionary dic, string model) { | |
if (dic[model] != null) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment