Я использую привязку пользовательской модели в веб-интерфейсе, но значение моей модели равно нулю.
Невозможно получить значение из ValueProvider
.
Пожалуйста, посмотрите на мой код ниже.
bindingContext.ValueProvider.GetValue("Report") is null
Вот мой код.
public class TestReportDto
{
public ReportFormatType ExportFormat { get; set; }
public string Report { get; set; }
public Dictionary<string, object> ParameterValues { get; set; }
}
public enum ReportFormatType
{
PDF,
XLS
}
Класс моей модели Binder.
public class TestModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var testReportDto = new TestReportDto();
bool isSuccess = true;
if (bindingContext.ValueProvider.GetValue("Report") != null)
{
testReportDto.Report = Convert.ToString(bindingContext.ValueProvider.GetValue("Report").RawValue);
}
else
{
isSuccess = false;
bindingContext.ModelState.AddModelError("request.Report", "Report");
}
bindingContext.Model = testReportDto;
return isSuccess;
}
}
Код API:
public async Task<string> Post([ModelBinder(typeof(TestModelBinder))]TestReportDto request)
{
return "Hello";
}