2013-08-04 2 views
1

У меня есть модель, которую я хочу использовать для связи с внешним веб-сервисом. Предполагается, что на моем веб-сайте будет указано конкретное действие по почте.Параметры параметров печати для модели

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

public ActionResult Confirmation(ConfirmationModel){ 
... 
} 

Проблема в том, что имена параметров, которые они передают, не очень удобочитаемы для человека. И я хочу сопоставить их с моей более читаемой моделью.

't_numb' ====> 'TransactionNumber' 

Это можно сделать автоматически? Возможно, с атрибутом? Какой здесь лучший подход?

+0

Проверить http://stackoverflow.com/questions/4316301/asp-net -mvc-2-Bind-а-модель-свойство-к-а-разного название значение/4316327 # 4316327 – haim770

ответ

1

Создание модели связующего:

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) 

Вы должны увидеть значение t_numb Появляется в TransactionNumber Недвижимость viewmodel.

Смежные вопросы