0

I've gotten this from the forum already, but one of the answers provided a way to use search parameters in a url string. Some of my tables are too big to load in a browser, so apparently I will have to find out how to add search parameters to this code. The only reason I didn't use the other code was that it showed how to do that with a calendar.
I know nothing about JSON/jQuery/razor/c#. Please help.

@{
    var db = Database.Open("LGOnline");
    var result = db.Query("SELECT * FROM CashOS");
    var data = result.Select(x => new
    {
        ID = x.ID,
        STORE_NO = x.STORE_NO,
        DATE = x.DATE,
        MWS_AMOUNT = x.MWS_AMOUNT,
        FINAL_AMOUNT = x.FINAL_AMOUNT
    }).ToArray();

    Json.Write(data, Response.Output);
    Response.ContentType = "application/json";
}

Update: I am using Microsoft WebMatrix 3. I am able to get the data from my tables with this code and it converts them to JSON. I just don't need all of it at once.

Update: I got it to do what I was trying to do. It isn't pretty, and I am open for any number of suggestions, but this made it to where I can type in the url and add &STORE_NO=55 and also i can select a date if I want to.

@{
    var db1 = Database.Open("LGOnline");
    var formValue2 = Request.Form["STORE_NO"];
    var formValue3 = Request.Form["DATE"];
    if (IsPost)
    {
        Response.Redirect("test.cshtml?&STORE_NO=" + formValue2 + "&DATE=" + formValue3);
    }
    var Keyword2 = Request.QueryString["STORE_NO"];    //Retrieves passed variable from the database search page for STORE_NO
    var Keyword3 = Request.QueryString["DATE"];    //Retrieves passed variable from the database search page for DATE

    var sqlQ = "SELECT * FROM CashOS WHERE STORE_NO LIKE @0 AND DATE LIKE @1";
    var dataQ = db1.Query(sqlQ, "%" + Keyword2 + "%", "%" + Keyword3 + "%");  
    var requestedData = dataQ.Select(x => new
    {
        ID = x.ID,
        STORE_NO = x.STORE_NO,
        DATE = x.DATE,
        MWS_AMOUNT = x.MWS_AMOUNT,
        FINAL_AMOUNT = x.FINAL_AMOUNT
    }).ToArray();

    Json.Write(dataQ, Response.Output);
    Response.ContentType = "application/json";
}

This is the output by the way: [{"ID":28,"STORE_NO":55,"DATE":"/Date(1442811600000)/","MWS_AMOUNT":10.1600,"FINAL_AMOUNT":10.1600}]

Thank You all for your help!!

5
  • 1
    The whole point of MVC is to separate the model from the view. Create your model (data) first in the controller then pass to the view. How are you going to manage this with no knowledge of C# etc. Commented Jan 13, 2016 at 15:33
  • Oh I guess I should have said that I am using WebMatrix also Commented Jan 13, 2016 at 15:40
  • OMG. Am I understand rigth that this is View code? I mean creade DBContext on View? That's the best! Commented Jan 13, 2016 at 15:41
  • I guess it is. Since I'm not using the MVC model to create this, but this code is basically copied from Darin Dimitrov's post on this forum: stackoverflow.com/questions/9416380/…. Commented Jan 13, 2016 at 15:46
  • Look at this for various different ways of using parameterised queries Commented Jan 13, 2016 at 17:24

0

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.