开发者

Entity ID as input model field vs action parameter?

开发者 https://www.devze.com 2023-02-28 21:08 出处:网络
Should an input model for creating or updating entities have an ID field to identify the entity, or should your edit action accept an ID parameter?

Should an input model for creating or updating entities have an ID field to identify the entity, or should your edit action accept an ID parameter?

Compare

Input Model

[HttpPost]
public ActionResult(EntityInputModel input)
{
    var entity = _unitOfWork.CurrenSession.Get<MyEntity>(input.Id);
    // do editing
    // ...
}

Action Parameter

[HttpPost]
public ActionResult(Guid id, EntityInputModel input)
{
    var entity = _unitOfWork.CurrenSession.G开发者_如何学Goet<MyEntity>(id);
    // ...
}


Personally I prefer the first. I always define a specific view model for each POST action. So if this action requires an id I include it as part of this specific view model.

0

精彩评论

暂无评论...
验证码 换一张
取 消