0

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;}
}
Alexandre Rondeau
  • 2,667
  • 24
  • 31
  • I've already tried adding the MyReportModel as a parameter to the GetReportSnapshot method, but that just doesn't work – Alexandre Rondeau Jul 23 '13 at 19:16
  • "all my parameters are inside of the URL". Does that mean they are in the query string? If that's the case, the reporting engine is not calling the Action via POST. Please provide code for your View. – ataravati Jul 23 '13 at 19:52
  • I do not have the code for the View, the call is made from the third party. The parameters aren't in the Query strings, but they are in the Url, I can get to those parameters only in the HttpContext.Request.UrlReferrer – Alexandre Rondeau Jul 23 '13 at 19:59
  • Can you give an example of how the parameters are passed into your Action in a URL? – ataravati Jul 23 '13 at 20:01
  • In the action the HttpContext.Request.UrlReferrer contains: https://dev/Report/Index/1?prepareby=Daboododo&preparedat=07%2F02%2F2013 using the HttpUtility I can parse that into a queyrstring, but those values aren't in the querystring during the "ModelBinding phase" since it's a post to dev/Report/Index/1 – Alexandre Rondeau Jul 23 '13 at 20:10
  • Can you get the values from your ValueProvider in your action? Just use `var valueProvider = new UrlReferrerValueProvider(ControllerContext);`, and try to get the values. – ataravati Jul 23 '13 at 20:29
  • Yes this works, my UrlReferrerValueProvider is functional. It's using it with the DefaultModelBinder that raises a null ref – Alexandre Rondeau Jul 23 '13 at 20:33

1 Answers1

1

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();
}
Charlie Brown
  • 2,817
  • 2
  • 20
  • 31
  • This doesn't work because the parameters aren't sent as part of the query string nor the form, they are only inside of the UrlReferrer. Plus, The Url Referrer isn't filled at the "model binding phase" so I can't just add the UrlReferrerValueProvider inside of the BindingContent in the DefaultModelBinder – Alexandre Rondeau Jul 24 '13 at 11:26
  • Thanks, I missed that somehow. Edited my answer to reflect using the UrlReferrer. – Charlie Brown Jul 24 '13 at 14:24
  • Thanks, UpdateModel that is the part that I was missing! – Alexandre Rondeau Jul 24 '13 at 18:22