状況は、パスワードを変更するなどの方法で、コントロールパネルを表示して別に多くのデータを含む複雑なモデルがあるという状況です。複数のモデルを表示して1つしか投稿する
もう1つの大きなモデルが提出されます。
情報大きなモデルが検証
public class PasswordChangeModel {
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "OldPassword")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Repeat password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string RepeatPassword { get; set; }
}
コントローラCatching-/wのパスワードモデルをロードを必要とし、POSTing
時に必要とされていないモデル
public class ProfileModel {
// This is the submitted model:
public PasswordChangeModel Password = new PasswordChangeModel();
// Personal Info
public string Name {get; set;}
public string LastName {get; set;}
// 15~ more fields
}
内アクション
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ChangePassword(PasswordChangeModel model) {
if (!ModelState.IsValid) //validate the model
return View(model);
//do stuff ...
return Index();
}
HTMLは、フォームを生成する
<form asp-controller="Profile" asp-action="ChangePassword" asp-antiforgery="true">
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<label asp-for="Password.OldPassword">Old Password</label>
<input asp-for="Password.OldPassword"/>
<label asp-for="Password.Password">New Password</label>
<input asp-for="Password.Password"/>
<label asp-for="Password.RepeatPassword">New Password Repeat</label>
<input asp-for="Password.RepeatPassword"/>
<input type="submit" class="btn" name="submit" value="Change"/>
</form>
質問
今のコードを検討した後、私の質問は - それがあれば、そのように提出することが可能ですそれを行うための最も便利でクリーンな方法は何ですか?
注:私は常にパスワードの変更ProfileModel
の中に3つのフィールドを含めることができますが、A-Itは醜いですし、B-ItはまだProfileModelデータ全体を読み込みます。
これに部分的なビューを使用できない理由はありますか? PasswordChangeModelをマスタービューの一部として使用することはできますが、アトラスでは1つのビューで2つのモデルを使用しないようにもう少し分けることができます。 – Marqueone