Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Convert Stream into model object in Asp.net mvc

$
0
0

hey i am sending request to web api where api process my request and return data from db to my viewmodel 

where as in mvc project i am getting the result in stream please provide me the way to convert that stream to object so that i cant return to view

here is the code for mvc project

 public ActionResult Index()
        {
            QuestionsViewModel Qvm = new QuestionsViewModel();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:6760/api/values/Index");
            request.Method = "GET";

            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            // Clean up the streams and the response.
            reader.Close();
            response.Close();
            
            return View(responseFromServer);


        }

and here is the code for api project

 public QuestionsViewModel Index()
        {
            //string uName = User.Identity.GetUserName();
            QuestionsViewModel vm = new QuestionsViewModel();

            AddQuestion adq = new AddQuestion();
            //var Sessionid = System.Guid.NewGuid();
            //vm.sessionid = Sessionid.ToString();

            //Session["ApplicantSession"] = Sessionid.ToString();

            //ViewBag.StartTime = Session.Timeout;

            List<List<int>> threecompQids = new List<List<int>>();
            List<int> c1question = db.AddQuestions.Where(x => x.ComplexityLevel == 1)
                .Select(y => y.AddQuestionID).ToList();
            List<int> c2question = db.AddQuestions.Where(x => x.ComplexityLevel == 2)
                .Select(y => y.AddQuestionID).ToList();
            List<int> c3question = db.AddQuestions.Where(x => x.ComplexityLevel == 3)
              .Select(y => y.AddQuestionID).ToList();
            List<int> rn = new List<int>();
            List<int> rn2 = new List<int>();
            List<int> rn3 = new List<int>();
            for (int i = 0; i < 1; i++)
            {
                int r = rnd.Next(c1question.Min(), c1question.Max() + 1);
                while (!(c1question.Any(w => w.Equals(r)) && !rn.Any(w => w == r)))
                {
                    r = rnd.Next(c1question.Min(), c1question.Max() + 1);
                }
                rn.Add(r);
                r = rnd.Next(c2question.Min(), c2question.Max() + 1);
                while (!(c2question.Any(w => w.Equals(r)) && !rn2.Any(w => w == r)))
                {
                    r = rnd.Next(c2question.Min(), c2question.Max() + 1);
                }
                rn2.Add(r);
                r = rnd.Next(c3question.Min(), c3question.Max() + 1);
                while (!(c3question.Any(w => w.Equals(r)) && !rn3.Any(w => w == r)))
                {
                    r = rnd.Next(c3question.Min(), c3question.Max() + 1);
                }
                rn3.Add(r);

            }
            var fstquestion = rn[0];
            threecompQids.Add(rn);
            threecompQids.Add(rn2);
            threecompQids.Add(rn3);

            vm.ComplexLevQidsLists = threecompQids;
            int addQuestID = db.AddQuestions.SingleOrDefault(x => x.AddQuestionID == fstquestion).AddQuestionID;
            List<Option> opt = db.Options.Where(op => op.AddQuestionID == addQuestID).ToList();
            vm.Questions = db.AddQuestions.SingleOrDefault(x => x.AddQuestionID == fstquestion);
            vm.Questions.CorrectAnswer = null;
            vm.Questions.Hint = null;
            vm.Questions.Explanation = null;
            //vm.UserName = uName;

            return vm;

        }


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>