3

In my Controller , i have Action ,which i want/try to return Json , but im not sure why i get this error , did i miss something ?! Can anyone point me in right direction! thanks in advance :)

RMAHistorik(LikeOrderNummer, From, Amount, SearchRMA, skip).Select;
System.Web.Mvc.JsonResult' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Web.Mvc.JsonResult' could be found (are you missing a using directive or an assembly reference?)

Controller :

public ActionResult RMAHistory()
{
    return View(RMAHistorik("", 0, 10, true, 0));
}

[HttpGet]
public JsonResult RMAHistorik(string LikeOrderNummer, int From, int Amount, bool SearchRMA, int skip)
{

    RMAHistory rma = new RMAHistory();
    string EmailID = Session["Email"].ToString();

    var query = db.RMAStatus.Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y }).Where(a => a.y.Email == EmailID && LikeOrderNummer == "" ? true : a.y.Ordrenummer.StartsWith(LikeOrderNummer.Trim()) || a.y.Fakturnummer.StartsWith(LikeOrderNummer.Trim())).Distinct().Select(t => new RMAHistory
    {
        Status = t.u.Status,
        RMASendDato = t.y.RMASendDato,
    }).OrderByDescending(t => t.OrdreDato).Skip(skip).Take(Amount).ToList();

    return Json(query,JsonRequestBehavior.AllowGet); //Error here
}

/////////////////////////////// update

public string JsonRMAHistory(string LikeOrderNummer, int From, int Amount, bool SearchRMA, bool Searching, int skip)
{
    if (Searching)
    {
        skip = 0;
    }

    string EmailID = Session["Email"].ToString();
    return Newtonsoft.Json.JsonConvert.SerializeObject(RMAHistorik(LikeOrderNummer, From, Amount, SearchRMA, skip).Select(t => new
    {
        RMASendDato = t.RMASendDato.ToString("dd/MM/yyy"),
        OrdreDato = t.OrdreDato.ToString("dd/MM/yyy"),
        Varenummer = t.Varenummer,
        Referencenummer = t.Referencenummer,
        AntalRMA = t.AntalRMA,
        Fakturnummer = t.Fakturnummer,
        Ordrenummer = t.Ordrenummer,
        Status = t.Status,
        Email = EmailID

    }).Where(l => l.Email == EmailID).Distinct());
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
7 seconds
  • 133
  • 2
  • 17
  • 2
    Because you method needs to be `public JsonResult RMAHistorik(...)` - that is what your method returns, not `IEnumerable –  Jun 13 '18 at 07:29
  • @StephenMuecke I thought i didnt need that :) but yeah you right i fix that , but i get another error from another Action , i update my question , would you please take quick look , thanks :) – 7 seconds Jun 13 '18 at 07:34
  • @StephenMuecke i just update my question :) :) :) – 7 seconds Jun 13 '18 at 07:38
  • why are you converting the data to JSON and then immediately converting it back again? This really makes no sense. – ADyson Jun 13 '18 at 07:59
  • @ADyson yes you right, i just confused myself :) is there way i can get out of this! :) – 7 seconds Jun 13 '18 at 08:03
  • Instead make a separate method which exposes the query functionality you want to share between both action methods, and can be re-used because it returns a linq object rather than JSON or HTML. Only the action methods which go directly to the user should return those types of data – ADyson Jun 13 '18 at 08:14
  • @ADyson thanks i will try that and if its possible would please share some code otherwise thanks again :) – 7 seconds Jun 13 '18 at 08:48
  • It's hard because your re-usable method would ideally need to return the result of the line `var query = db.RMAStatus.Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y }).Where(a => a.y.Email == EmailID && LikeOrderNummer == "" ? true` etc but since you declared it as `var` I don't know what the real type of `query` is. Methods can't return `var` AFAIK. Can you tell me the please? If you don't know, Intellisense should be able to tell you. – ADyson Jun 13 '18 at 08:51
  • @ADyson if i understand correctly type of query is generic list :) – 7 seconds Jun 13 '18 at 08:56
  • yes but it must be a generic list _of_ something? e.g. `List`. It can't just be `List<>` – ADyson Jun 13 '18 at 09:01
  • Yes sorry its like this > List – 7 seconds Jun 13 '18 at 09:04
  • I made an answer which I hope is helpful. I wasn't sure how your 3rd `public string JsonRMAHistory` method fits in, so I've ignored it. It wasn't clear if this was another method needed to return a slightly different set of data, or an attempt to replace the RMAHistorik method, or what. I can incorporate that into my answer if it's needed, and if you can clarify its status. – ADyson Jun 13 '18 at 09:34

2 Answers2

0

You cannot call a Select() method on a jsonResult. You could something like below. Let me know if this helps!!

public ActionResult RMAHistory()
{
    return View(RMAHistorik("", 0, 10, true, 0));
}

[HttpGet]
public JsonResult RMAHistorik(string LikeOrderNummer, int From, int Amount, bool SearchRMA, int skip)
{
    var query = GetData(LikeOrderNummer, Amount, skip).ToList();

    return Json(query,JsonRequestBehavior.AllowGet);
}

public string JsonRMAHistory(string LikeOrderNummer, int From, int Amount, bool SearchRMA, bool Searching, int skip)
{
    if (Searching)
    {
        skip = 0;
    }

    string EmailID = Session["Email"].ToString();
    return Newtonsoft.Json.JsonConvert.SerializeObject(GetData(LikeOrderNummer, Amount, skip).Select(t => new
    {
        RMASendDato = t.RMASendDato.ToString("dd/MM/yyy"),
        OrdreDato = t.OrdreDato.ToString("dd/MM/yyy"),
        Varenummer = t.Varenummer,
        Referencenummer = t.Referencenummer,
        AntalRMA = t.AntalRMA,
        Fakturnummer = t.Fakturnummer,
        Ordrenummer = t.Ordrenummer,
        Status = t.Status,
        Email = EmailID

    }).Where(l => l.Email == EmailID).Distinct());
}

private IEnumerable<RMAHistory> GetData(string LikeOrderNummer, int Amount, int skip)
{
    RMAHistory rma = new RMAHistory();
    string EmailID = Convert.ToString(HttpContext.Current.Session["Email"]);

    return db.RMAStatus.Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y }).Where(a => a.y.Email == EmailID && LikeOrderNummer == "" ? true : a.y.Ordrenummer.StartsWith(LikeOrderNummer.Trim()) || a.y.Fakturnummer.StartsWith(LikeOrderNummer.Trim())).Distinct().Select(t => new RMAHistory
    {
        Status = t.u.Status,
        RMASendDato = t.y.RMASendDato,

    }).OrderByDescending(t => t.OrdreDato).Skip(skip).Take(Amount).ToList();
}
User4179525
  • 516
  • 1
  • 4
  • 18
  • Hi Sathish , i get 2 errors 1.under SerializeObject(GetData(LikeOrderNummer, From, Amount, SearchRMA, skip) and its about No Overload for method 'GetData' takes 5 arguments and number 2. in private static method under Session and is about An object reference is requried for non-static field,method or property 'System.Web.MVC.Controller.Session.get' – 7 seconds Jun 13 '18 at 08:46
  • @7seconds Could you please make sure you have made changes as per my post above. 1. I have changed SerializeObject(GetData(LikeOrderNummer, From, Amount, SearchRMA, skip) => GetData(LikeOrderNummer, Amount, skip). 2. Please use string EmailID = Session["Email"].ToString(); in static method – User4179525 Jun 13 '18 at 09:00
  • Same Error For Session , i cant use Session in private static method – 7 seconds Jun 13 '18 at 09:12
  • @7seconds Could you please use this HttpContext.Current.Session["Email"] instead of Session["Email"]. reference: https://stackoverflow.com/questions/2577183/how-can-i-get-the-value-of-a-session-variable-inside-a-static-method – User4179525 Jun 13 '18 at 09:14
  • when i use string EmailID = Convert.ToString(Session["Email"]); i get same error as before but it also write something about 'Get the HttpSessionStateBase object for the current HTTP request' and the error. and yes i also use that but again same error An object reference is requried for non-static field,method or property 'System.Web.MVC.Controller.Session.get' – 7 seconds Jun 13 '18 at 09:21
  • Could you try changing it to System.Web.HttpContext.Current.Session["Email"] and let me know if you still having issues – User4179525 Jun 13 '18 at 09:34
  • I just removed static and session going to worked and also thanks for help :) – 7 seconds Jun 13 '18 at 10:02
0

Converting the data to JSON and then immediately converting it back again, as you are doing, makes no sense - it's not efficient and it's not helping you with the problem you've got.

Instead make a separate method which exposes the query functionality you want to share between both action methods, and can be re-used. This abstracts out the execution of the query and creation of the result set from the format in which is it returned to the user.

public ActionResult RMAHistory()
{
    return View(getRMAHistory("", 0, 10, 0));
}

[HttpGet]
public JsonResult RMAHistorik(string LikeOrderNummer, int From, int Amount, int skip)
{
   List<RMAHistory> results = getRMAHistory(LikeOrderNummer, From, Amount, skip);
   return Json(results, JsonRequestBehavior.AllowGet);
}

//private method to allow RMAHistory query to be re-used in both action methods
private List<RMAHistory> getRMAHistory(string LikeOrderNummer, int From, int Amount, int skip)
{
    string EmailID = Session["Email"].ToString();

    List<RMAHistory> query = db.RMAStatus
      .Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y })
      .Where(a => a.y.Email == EmailID && LikeOrderNummer == "" ? true : a.y.Ordrenummer.StartsWith(LikeOrderNummer.Trim()) || a.y.Fakturnummer.StartsWith(LikeOrderNummer.Trim()))
      .Distinct()
      .Select(t => new RMAHistory
      {
          Status = t.u.Status,
          RMASendDato = t.y.RMASendDato,
      })
      .OrderByDescending(t => t.OrdreDato)
      .Skip(skip)
      .Take(Amount)
      .ToList();

    return query;
}

P.S. I have assumed that the model for the RMAHistory View is List<RMAHistory> or IEnumerable<RMAHistory>, and it is not expecting some JSON string.

ADyson
  • 57,178
  • 14
  • 51
  • 63