2

エンドポイントを作成する必要がありますが、このエンドポイントには複数のタイプの入力があり、フォーム自体はconfigに基づいて変更できるため、できるだけ2つ以上のオブジェクトを作成しようとしていました入力。以下のようなWebApi 2つの異なるオブジェクトを持つモデルバインド

何か:

public class ParticipationsController : ApiController 
{ 

public HttpResponseMessage Post([FromBody]Models.SimpleParticipationModel sModel, [FromBody]Models.CompleteParticipationModel cModel) 
    { 
     if (!ModelState.IsValid) // this might not be this way here 
     { 
      return Request.CreateResponse(HttpStatusCode.BadRequest); 
     } 

     return Request.CreateResponse(HttpStatusCode.OK, "Ok"); 
    } 

私のポイントは、複数のエンドポイントを持つ回避し、ページ内のrenderizationの多くを変更することです。

オブジェクトには、「必須」や「範囲0-X」などの特定の規則に従うDataAnotationsが含まれています。

私はまた、すべてのプロパティを持つオブジェクトを持っていないし、それらのいくつかを満たすだけでした。

ありがとうございます。

答えて

1

これはおそらく不可能です。上記のオブジェクトの両方を含むオブジェクトまたは作成オブジェクトのそれぞれに対して2つのエンドポイントを作成します。

たとえば、基本的に両方のオブジェクトを含むAPIアクションでViewModelのオブジェクトを渡すことができます。これにより、オブジェクトのプロパティに対するData Annotationの動作も維持されます。

public class ViewModel 
{ 
    SimpleParticipationModel sModel {get;set;} 
    CompleteParticipationModel cModel {get;set;} 
} 

public class ParticipationsController : ApiController 
{ 

public HttpResponseMessage Post([FromBody]ViewModel) 
{ 
    if (!ModelState.IsValid) // this might not be this way here 
    { 
     return Request.CreateResponse(HttpStatusCode.BadRequest); 
    } 

    return Request.CreateResponse(HttpStatusCode.OK, "Ok"); 
} 
関連する問題