0

I've already made a stored procedure and I've called it in Controller, then the next problem is when I called this method below in view class I got error, anyone can help ?

public ListResponse<ViewStoredGraphRow> GetDataGraph()
{
    using (var connection = SqlConnections.NewFor<TryGraphRow>())
    {
       var data = connection.Query<Item>("StoreGraph",
            param: new
            {
                StartDate = 2018 - 07 - 07,
                EndDate = 2018 - 07 - 09
            },
            commandType: System.Data.CommandType.StoredProcedure);

        TestPesan tp = new TestPesan();

        var response = new ListResponse<ViewStoredGraphRow>();
        response.Entities = (List<ViewStoredGraphRow>)data;

        return response;
    }
}

and this is my error

enter image description here

Thanks!

1
  • Avoid putting custom methods that belongs to view page inside controller classes (they're not intended for instantiation with new keyword). You can declare another class and put GetDataGraph() method there. Commented Jul 12, 2018 at 7:59

1 Answer 1

1

No problem. Once your view is rendering, your controller has already been instantiated. You don't need another instance of it, just grab a reference to the existing one. So, instead of doing this in your view:

var data = new DashboardController().GetDataGraph();

Do this:

var controller = ViewContext.Controller as DashboardController;
var data = controller.GetDataGraph();

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.