2012-04-04 2 views
1

ID 값을 모델에 다시 바인딩되는 첫 번째 것으로하고 싶다는 점을 제외하고는 내 뷰에서 성공적으로 모델을 전달할 수 있습니다. 그래서 그것은 db로부터 어떤 정보를 채울 수 있습니다.MVC3에서 모델에 배치 된 객체의 순서 및 제어 방법

[HttpPostAttribute] 
public ActionResult SaveSetup(SetupAggregate setup) 
{ 
    setup.SaveSetup(); 
    return null; 
} 

다음 나는 SetupID가 빈 설정 개체에 설정된 첫 번째 특성 싶습니다으로

SetupID:91c16e34-cf7d-e111-9b66-d067e53b2ed6 
SwayBarLinkLengthLF: 
SwayBarLinkLengthRF: 
.....way more information.... 

내 작업은 다음과하지만이처럼 보이는 정보는 다시 전달되는 첫 번째 속성이 사전 순으로 설정됩니다.

+0

사용자 지정 ModelBinder를 만들어야 할 수도 있습니다 필요합니다. –

+0

이 양식은 많은 양의 데이터를 게시합니다. 어쨌든 모든 속성을 설정하지 않고도 사용자 정의 모델 바인더를 수행 할 수 있습니까? I.E. 내가 설정할 수있는 속성을 설정하고 나머지는 기본 처리하도록 하시겠습니까? – PlTaylor

+0

기본 공급자를 상속하면 대부분 그렇습니다. 이 스레드를 확인하십시오 : http://stackoverflow.com/questions/3636747/asp-net-mvc-custom-model-binder-for-id-fields 목표는 기본 모델 바인더에서 상속하고 ID 필드를 채 웁니다. 먼저, base.BindModel (controllerContext, bindingContext)을 사용하십시오. –

답변

0

제 의견으로는 MVC 프로젝트에있는 ViewModel과 뷰에 렌더링되는 두 개의 "모델"로 작업해야합니다. 그리고 비즈니스 로직 계층의 두 번째 EntityModel. 이것은 표준 "엔터프라이즈"프로그래밍 설계입니다. 그것은 당신에게 당신의 데이터에 대한 더 많은 통제를 제공합니다. 아이디어는 이것입니다.

UI 조립 (MVC 프로젝트)

뷰 모델 정의

public class MyModel { 
    public int ID { get; set; } 
    .... // bunch of other properties 
} 

컨트롤러

public class InterestingController : Controller { 

    public ActionResult CreateNewWidget() { 
     var model = new MyModel(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult CreateNewWidget(MyModel model) { 
     if(ModelState.IsValid) { 
      // your ctor can define the order of your properties being sent in and you can set the entity values in the ctor body however you choose to. Note never SET an ID/Primary key on a Create, let the DB handle that. If you need to return the new Key value, get it from the insert proc method in your DAL and return it up the stack 
      var entityModel = new EntityFromBLL(model.Name, model.OtherProperty, ... etc); 
      entityModel.Save(User.Identity.Name); // your save method should always capture WHO is doing the action 
     } 
     return View(model); 
    } 

    public ActionResult UpdateExistingWidget(int id) { 
     var entityModel = new EntityFromBLL(id); // get the existing entity from the DB 
     var model = new MyModel(entityModel.ID, entityModel.Name, ... etc); // populate your ViewModel with your EntityModel data in the ViewModel ctor - note remember to also create a parameterless default ctor in your ViewModel as well anytime you create a ctor in a ViewModel that accepts parameters 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult UpdateExistingWidget(MyModel model) { 
     if(ModelState.IsValid) { 
      var entityModel = new EntityFromBLL(model.ID); // always pull back your original data from the DB, in case you deal with concurrency issues 
      // now go thru and update the EntityModel with your new ViewModel data 
      entityModel.Name = model.Name; 
      //... etc set all the rest of the properties 
      // then call the save 
      entityModel.Save(User.Identity.Name); 
     } 
     return View(model) 
    } 
} 
귀하의 엔티티 모델은 민간 분야, 공용 속성, 모든 소요 ctor에 정의되어야한다

삽입 (기본 키 빼기)을위한 필수 필드, 기본 키를 가져 와서 내부로드 메소드를 정적으로 호출하여 채워진 객체를 반환 할 수있는 ctor 목적. 비즈니스 규칙 및 속성 유효성 검사 및 단일 Save 메서드 Save 메서드는 모든 속성을 설정 한 후 IsDirty 비트를 확인하고 해당 Insert 또는 Update 메서드를 호출해야합니다. 그러면 DTO를 전달하는 DAL을 차례로 호출해야합니다.

+1

나는 당신이 묘사 한 것과 매우 비슷한 것을 사용했다. 나는 그것을 모두 내 viewmodel에서 처리했다. 방금 viewmodel의 다른 인스턴스를 허용하고 거기에서 논리를 수행 한 내 viewmodel의 생성자를 만들었습니다. – PlTaylor

+0

N 계층 구조를 사용하면 문제가 해결되었다는 것을 실제로 생각하고있었습니다. 매우 멋졌습니다! –

관련 문제