创建模型绑定器:
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)
您应该会在viewmodel 的属性中看到t_numb
出现的值。TransactionNumber