回答編集履歴
2
追記
test
CHANGED
@@ -7,3 +7,293 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
あと、未検証ですが、00:mm:ss という形でテキストボックスに入力する必要がありそうです。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
**【追記】**
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
ちょっと検証してみました。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
テキストボックスの表示は mm:ss としても、それを送信してサーバー側で受けるときは hh:mm と解釈されるのが問題です。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
つまり、例えば、テキストボックスに 30:00 とか 59:59 とか mm:ss 形式であれば有効な数字を入力しても、サーバー側では hh:mm 形式と解釈され、30 とか 59 は hh としては不正なのでモデルバインディングできずエラーになります。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
なので、TextBoxFor の第 2 引数に "{0:mm\:ss}" を設定するのは mm:ss 形式で表示するだけならいいかもしれませんが、その後ユーザーが編集して送信ということが必要なら、送信する前に 00:mm:ss にしなければならないということで、操作が面倒になるだけなので意味がなさそうです。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
TextBoxFor の第 2 引数は "{0:hh\:mm\:ss}" と設定しで最初から hh:mm:ss 形式で表示し、00 分 00 秒以上、59 分 59 秒以下という範囲を超えたらエラーを出すにはデータアノテーション属性(RegularExpression または Range)を付与することで対応するのをお勧めします。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
ご参考までに検証に使ったコードをアップしておきます。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
**Model**
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
using System;
|
48
|
+
|
49
|
+
using System.Collections.Generic;
|
50
|
+
|
51
|
+
using System.Linq;
|
52
|
+
|
53
|
+
using System.Web;
|
54
|
+
|
55
|
+
using System.ComponentModel.DataAnnotations;
|
56
|
+
|
57
|
+
using System.ComponentModel.DataAnnotations.Schema;
|
58
|
+
|
59
|
+
using System.Globalization;
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
namespace Mvc5App.Models
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
public class TimeSpanModel
|
68
|
+
|
69
|
+
{
|
70
|
+
|
71
|
+
[Required]
|
72
|
+
|
73
|
+
// クライアント側の検証を無効にすれば RangeAttribute でも可
|
74
|
+
|
75
|
+
//[Range(typeof(TimeSpan), "00:00:00", "00:59:59")]
|
76
|
+
|
77
|
+
[RegularExpression(@"^00:[0-5]\d:[0-5]\d$")]
|
78
|
+
|
79
|
+
public TimeSpan Start { set; get; }
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
[Required]
|
84
|
+
|
85
|
+
// クライアント側の検証を無効にすれば RangeAttribute でも可
|
86
|
+
|
87
|
+
//[Range(typeof(TimeSpan), "00:00:00", "00:59:59")]
|
88
|
+
|
89
|
+
[RegularExpression(@"^00:[0-5]\d:[0-5]\d$")]
|
90
|
+
|
91
|
+
public TimeSpan Stop { set; get; }
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
**View**
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
@model Mvc5App.Models.TimeSpanModel
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
@{
|
112
|
+
|
113
|
+
ViewBag.Title = "TimeSpanTest";
|
114
|
+
|
115
|
+
Layout = "~/Views/Shared/_Layout.cshtml";
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
// クライアント側での検証を無効にするには以下のコメントアウトを解除
|
120
|
+
|
121
|
+
//Html.EnableClientValidation(false);
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
<h2>TimeSpanTest</h2>
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
@using (Html.BeginForm())
|
132
|
+
|
133
|
+
{
|
134
|
+
|
135
|
+
@Html.AntiForgeryToken()
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
<div class="form-horizontal">
|
140
|
+
|
141
|
+
<h4>TimeSpanModel</h4>
|
142
|
+
|
143
|
+
<hr />
|
144
|
+
|
145
|
+
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
|
146
|
+
|
147
|
+
<div class="form-group">
|
148
|
+
|
149
|
+
@Html.LabelFor(model => model.Start, htmlAttributes: new { @class = "control-label col-md-2" })
|
150
|
+
|
151
|
+
<div class="col-md-10">
|
152
|
+
|
153
|
+
@Html.TextBoxFor(model => model.Start, "{0:hh\:mm\:ss}", new { @class = "form-control" })
|
154
|
+
|
155
|
+
@Html.ValidationMessageFor(model => model.Start, "", new { @class = "text-danger" })
|
156
|
+
|
157
|
+
</div>
|
158
|
+
|
159
|
+
</div>
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
<div class="form-group">
|
164
|
+
|
165
|
+
@Html.LabelFor(model => model.Stop, htmlAttributes: new { @class = "control-label col-md-2" })
|
166
|
+
|
167
|
+
<div class="col-md-10">
|
168
|
+
|
169
|
+
@Html.TextBoxFor(model => model.Stop, "{0:hh\:mm\:ss}", new { @class = "form-control" })
|
170
|
+
|
171
|
+
@Html.ValidationMessageFor(model => model.Stop, "", new { @class = "text-danger" })
|
172
|
+
|
173
|
+
</div>
|
174
|
+
|
175
|
+
</div>
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
<div class="form-group">
|
180
|
+
|
181
|
+
<div class="col-md-offset-2 col-md-10">
|
182
|
+
|
183
|
+
<input type="submit" value="Create" class="btn btn-default" />
|
184
|
+
|
185
|
+
</div>
|
186
|
+
|
187
|
+
</div>
|
188
|
+
|
189
|
+
</div>
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
<div>
|
196
|
+
|
197
|
+
@Html.ActionLink("Back to List", "Index")
|
198
|
+
|
199
|
+
</div>
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
@section Scripts {
|
204
|
+
|
205
|
+
@Scripts.Render("~/bundles/jqueryval")
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
**Controller**
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
```
|
218
|
+
|
219
|
+
using System;
|
220
|
+
|
221
|
+
using System.Collections.Generic;
|
222
|
+
|
223
|
+
using System.Linq;
|
224
|
+
|
225
|
+
using System.Web;
|
226
|
+
|
227
|
+
using System.Web.Mvc;
|
228
|
+
|
229
|
+
using System.ComponentModel.DataAnnotations;
|
230
|
+
|
231
|
+
using Mvc5App.Models;
|
232
|
+
|
233
|
+
using System.IO;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
namespace Mvc5App.Controllers
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
public class HomeController : Controller
|
242
|
+
|
243
|
+
{
|
244
|
+
|
245
|
+
public ActionResult TimeSpanTest()
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
TimeSpanModel model = new TimeSpanModel
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
Start = new TimeSpan(0, 0, 0),
|
254
|
+
|
255
|
+
Stop = new TimeSpan(0, 59, 59)
|
256
|
+
|
257
|
+
};
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
return View(model);
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
[HttpPost]
|
268
|
+
|
269
|
+
public ActionResult TimeSpanTest(TimeSpanModel model)
|
270
|
+
|
271
|
+
{
|
272
|
+
|
273
|
+
if (ModelState.IsValid)
|
274
|
+
|
275
|
+
{
|
276
|
+
|
277
|
+
return RedirectToAction("Index");
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
return View(model);
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
```
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
Model に付与した Rquired と RegularExpression での検証結果は以下のようになります。下の例ではエラーメッセージはデフォルトですが自由に設定できます。
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
![イメージ説明](2fa57c405eeba6aad3014a8bb9e7f39d.jpeg)
|
1
追記
test
CHANGED
@@ -3,3 +3,7 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
00 分 00 秒以上、59 分 59 秒以下という範囲を超えたらエラーを出すにはデータアノテーション属性(RegularExpression または Range)を付与することで可能です。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
あと、未検証ですが、00:mm:ss という形でテキストボックスに入力する必要がありそうです。
|