Question

I'm using a third party reporting engine (stimulsoft) that calls an action on a controller via POST. Inside of the form, many fields are sent for the mechanics of the third party. Inside of the action I need some parameters all my parameters are inside of the URL.

I want to be able to use the model binder inside of my action.

At the moment I'm getting each fields one by one using this methods

var queryString = HttpUtility.ParseQueryString(Request.UrlReferrer.Query);
var preparedBy = queryString["preparedBy"];
var preparedAt = (queryString["preparedAt"] != null) ? Convert.ToDateTime(queryString["preparedAt"]) : DateTime.Today;

I would prefer to use a model and binding using the UrlReferrer. I've created a UrlReferrerValueProvider to bind from the action. I've tried that, but I'm getting a NullReferenceException on binder.BindModel line

public class UrlReferrerValueProvider : NameValueCollectionValueProvider
{
    public UrlReferrerValueProvider(ControllerContext controllerContext)
        : base(HttpUtility.ParseQueryString(controllerContext.HttpContext.Request.UrlReferrer.Query), CultureInfo.InvariantCulture)
    {
    }
}

public ActionResultat GetReportSnapshot()
{
    var bindingContext = new ModelBindingContext()
        {
            ValueProvider = new UrlReferrerValueProvider(ControllerContext),
            ModelName = "MyReportModel",
            FallbackToEmptyPrefix = true
        };
    var binder = new DefaultModelBinder();
    var myReportModel = binder.BindModel(ControllerContext, bindingContext);

    [...]
    return new EmptyResult();
}

public class MyReportModel
{
    public string PreparedBy {get;set;}
    public DateTime PreparedAt {get;set;}
}
Was it helpful?

Solution

Edited based on comments.

public class MyReportModel
{
    public string PreparedBy {get;set;}
    public DateTime PreparedAt {get;set;}
}

public class UrlReferrerValueProvider : NameValueCollectionValueProvider
{
    public UrlReferrerValueProvider(ControllerContext controllerContext)
        : base(HttpUtility.ParseQueryString(controllerContext.HttpContext.Request.UrlReferrer.Query), CultureInfo.InvariantCulture)
    {
    }
}

public ActionResult GetReportSnapshot(MyReportModel model)
{
    this.UpdateModel(model, new UrlReferrerValueProvider(ControllerContext));

    return new EmptyResult();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top