我遇到的问题是当我尝试通过GetValue(“预算”)检索时,自定义模型绑定器为空,但正如您所看到的,绑定器包含前缀。如果我查看我有的元数据,我也得到所有的密钥,但仍然是NULL。任何人都可以解释为什么我得到NULL?
我的观点> List.cshtml
for (var i = 0; i < Model.Budget.BudgetRader.Count; i++)
{
if (Model.Budget.BudgetRader[i].Konto != -1) {
<tr id="@Model.Budget.BudgetRader[i].RadNr1 +'-'+ @Model.Budget.BudgetRader[i].RadNr2 +"-"+ @Model.Budget.BudgetRader[i].RadNr3">
<td>@Html.TextBoxFor(m => Model.Budget.BudgetRader[i].BudgetBelopp)</td>
</tr>}
}
我的ViewModel&gt; BudgetListViewModel
public class BudgetListViewModel
{
public Budget Budget { get; set; }
public IList<BudgetRad> BudgetRaderList { get; set; }
}
我的自定义模型粘合剂&gt; BudgetListModelBinder
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//var budgetListViewModel = (BudgetListViewModel) bindingContext.Model;
//true
var exists = bindingContext.ValueProvider.ContainsPrefix("Budget");
// gives null
var tmp = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
// gives null
var tmp2 = controllerContext.HttpContext.Request.Form.Get("Budget");
}
Globalasa
private void RegisterModelBinders()
{
ModelBinders.Binders.Add(typeof(BudgetListViewModel), new BudgetListModelBinder());
}