私はこのようなビューモデルを持っています-
public class upgradeViewModel
{
public Cart Cart { get; set; }
public RegisterModel RegisterModel { get; set; }
}
ビューモデルにはRegisterModel
、RegisterModel
モデルオブジェクトと呼ばれるプロパティが含まれています。これは次のようになります-
public class RegisterModel
{
[Required]
[Display(Name = "Your name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Your email address")]
public string Email { get; set; }
...
}
ビューモデルのプロパティを使用して、オブジェクトをモデルとしてRegisterModel
使用するビューにフォームを設定します。upgradeViewModel
フォームを送信すると、クライアント側の検証は取得されませんが、RegisterModel
オブジェクトのサーバー側の検証は取得されます。
目立たないクライアント側の検証が機能するようにすべてが正しく構成されていると思います。これは、Webサイトの他の領域でも機能します。ビューモデルの子オブジェクトに対してクライアント側の検証を機能させるために、何か特別なことをする必要がありますか?
これが私のビューコードです-
@model upgradeViewModel
@{
ViewBag.Title = "Title";
Layout = "~/Views/Shared/_LayoutNormal.cshtml";
}
<h2>Title</h2>
Some text ....
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Your Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.RegisterModel.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.RegisterModel.UserName)
@Html.ValidationMessageFor(m => m.RegisterModel.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.RegisterModel.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.RegisterModel.Email)
@Html.ValidationMessageFor(m => m.RegisterModel.Email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.RegisterModel.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.RegisterModel.Password)
@Html.ValidationMessageFor(m => m.RegisterModel.Password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
@Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword)
</div>
<p>
<input type="submit" value="Save Details" class="inputbutton" />
</p>
</fieldset>
</div>
}