これが可能かどうかはよくわかりませんが、確認したいと思いました。いくつかの異なるハンドラーメソッドがあるかみそりのページがあります。それらのいくつかでは、部分的なビューの結果を返します。
例:
public class BoardMeetingsModel : PageModel
{
//ctor
//properties
public IActionResult OnGetFetchCreateMeetingPartial()
{
return Partial("_CreateMeetingPartial", new ManipulationDto());
}
}
私の部分ビューは次のように設定されています。
@using Models.ManipulationModels
@model ManipulationDto
これは部分的なページなので、@ pageディレクティブを使用していません(部分的なページには名前が付けられてい_CreateMeetingPartial.cshtml
ます。ただし、ManipulationModelを渡すと、以下のエラーが発生します。
The model item passed into the ViewDataDictionary is of type 'Models.ManipulationDto', but this ViewDataDictionary instance requires a model item of type 'Pages.BoardMeetingsModel'.
私は自分のかみそりのページでパーシャルを呼んでいません。返されたデータをjavascriptモーダルで消費しているため、部分的なページを直接返しています。この動作をオーバーライドすることも可能ですか?デフォルトでは、ベースPageModel
(つまりBoardMeetingsModel)が渡されることを常に期待しています。
存在するモデルを明示的に渡しても、部分ビューに対して明示的に指定したモデルではなく、ページモデルを部分ビューが期待していることに驚いています。
上記の問題を解決するために、私は以下をしなければなりませんでした。ページに複数のモデルがあるため、ManipulationDtoプロパティに[BindProperty]属性がないことに注意してください。複数のモデルがあり、検証(必要なプロパティなど)がある場合、それらはすべてMVCとは異なるかみそりのページでトリガーされます。私の場合、それを処理する方法は、モデルをパラメーターとして直接渡すことでしたが、モデルの状態の検証が失敗した場合にすべての値を割り当てることができるパブリックプロパティがあることも確認しました。
それぞれが独自の検証を持つ複数の一意のモデルがない場合は、bindproperty属性を適用するだけで、心配する必要はありません。
public class BoardMeetingsModel : PageModel
{
//this gets initialized to a new empty object in the constructor (i.e. MeetingToManipulate = new ManipulationDto();)
public ManipulationDto MeetingToManipulate { get; set; }
//ctor
//properties
public IActionResult OnGetFetchCreateMeetingPartial(ManipulationDto meetingToManipulate)
{
//send the page model as the object as razor pages expects
//the page model as the object value for partial result
return Partial("_CreateMeetingPartial", this);
}
public async Task<IActionResult> OnPostCreateMeetingAsync(CancellationToken cancellationToken, ManipulationDto MeetingToCreate)
{
if (!ModelState.IsValid)
{
//set the pagemodel property to be the values submitted by the user
//not doing this will cause all model state errors to be lost
this.MeetingToManipulate = MeetingToCreate;
return Partial("_CreateMeetingPartial", this);
}
//else continue
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加