2013-08-04 3 views
1

외부 웹 서비스와의 통신에 사용할 모델이 있습니다. 내 웹 사이트에서 특정 게시물 작업을 호출해야합니다.모델에 매개 변수 게시

public class ConfirmationModel{ 
    ... 
    public string TransactionNumber {get; set;} 
} 

public ActionResult Confirmation(ConfirmationModel){ 
... 
} 

문제는 그들이 전달하는 매개 변수 이름이 사람이 읽을 수있는 것이 아니기 때문입니다. 그리고 그것들을 좀 더 읽기 쉬운 모델에 매핑하고 싶습니다.

't_numb' ====> 'TransactionNumber' 

자동으로 수행 할 수 있습니까? 어쩌면 속성이 있을까요? 가장 좋은 방법은 무엇입니까?

using System.Web.Mvc; 
using ModelBinder.Controllers; 

public class ConfirmationModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     var model = new ConfirmationModel(); 

     var transactionNumberParam = bindingContext.ValueProvider.GetValue("t_numb"); 

     if (transactionNumberParam != null) 
      model.TransactionNumber = transactionNumberParam.AttemptedValue; 

     return model; 
    } 
} 

이 Global.asax.cs에서 초기화를 : 당신의 액션 메소드에서 다음

protected void Application_Start() 
{ 
    ModelBinders.Binders.Add(typeof(ConfirmationModel), new ConfirmationModelBinder()); 
} 

당신의 가치를 볼 수

[HttpPost] 
public ActionResult Confirmation(ConfirmationModel viewModel) 

+0

확인 http://stackoverflow.com/questions/4316301/asp-net : 여기

public ActionResult Create(FormCollection values) { Recipe recipe = new Recipe(); recipe.Name = values["Name"]; // ... return View(); } 

하지만 다른 생각 모두에 대한 좋은 읽기입니다 -mvc-2-bind-a-models-속성과 다른 이름으로 지정된 값/4316327 # 4316327 – haim770

답변

1

하는 모델 바인더 만들기 t_numb은 뷰 모델의 TransactionNumber 속성에 표시됩니다.

관련 문제