Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(Controller)

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using OAGES5.Models;

namespace OAGES5.Controllers
{
    //[useStopwatch]
    public class ExamController : Controller
    {
        //
        // GET: /Exam/
        private QuestionareLevel1Entities1 l1t1 = new QuestionareLevel1Entities1();

        [HttpGet]
        public ActionResult AfterLogin()
        {
            if (Session["LogedUsedID"] == null)
            {
                return View();
                //return RedirectToAction("Page1");
            }
            else
            {
                return RedirectToAction("Login", "StudentLogin");
            }
        }
        [HttpPost]
        public ActionResult AfterLogin(Type1 t1)
        {
            if (!ModelState.IsValid)
            {
                return RedirectToAction("Login", "StudentLogin");
            }
            else
            {
                return RedirectToAction("Page1");
            }
           
        }
        
        [HttpGet]
        public ActionResult Page1()
        {
            Level1 t1 = new Level1();
            return View(t1);
        }
        [HttpPost]
        public ActionResult Page1(Level1 t1)
        {   
         
          
         
            var db = new QuestionareLevel1Entities1();
       // var ans = db.Type1.Where((x=> x.Id ==(1))).SingleOrDefault();
            var ans = db.Type1.Include(m => m.Id);
            ans = ans.Where(g => g.Id == 1); // g.Id is equal to option 1(1)
             if (String.IsNullOrEmpty(t1.SelectedAnswer))
            {
                return RedirectToAction("Page1");
            }
             
             else if (t1.SelectedAnswer.Equals(ans)) //Problem lies Here..
             {
                 ans = ans.Where(g => g.Id == 1); 
                
                 return RedirectToAction("Page2");// if answer is write
             }
             else
             {
                 return RedirectToAction("Page1");
             }
          
        }
        [HttpGet]
        public ActionResult Page2()
        {
            Level1 p2 = new Level1();
            return View(p2);
        }
        [HttpPost]
        public ActionResult Page2(Level1 p2)
        {
            if (String.IsNullOrEmpty(p2.SelectedAnswer))
            // if (!ModelState.IsValid)
            {
                return RedirectToAction("Page2");
            }

            else
            {
                return RedirectToAction("Page2");
            }
        }
    }
}
(View for Page 1)

HTML
@model OAGES5.Models.Level1

@{
    ViewBag.Title = "Page1";
}
<h2>Page1</h2>
@using (Html.BeginForm())
{
    foreach (var Type1 in Model.T1)
    {
        if (Type1.Id == 1)
        {
            @Html.DisplayFor(m => m.T1, Type1.Id) @Type1.QuestionName  <br />
    @Html.RadioButtonFor(m => m.SelectedAnswer, Type1.Id) @Type1.Option1<br />
    @Html.RadioButtonFor(m => m.SelectedAnswer, Type1.Id) @Type1.Option2<br />
    @Html.RadioButtonFor(m => m.SelectedAnswer, Type1.Id) @Type1.Option3<br />
    @Html.RadioButtonFor(m => m.SelectedAnswer, Type1.Id) @Type1.Option4<br />
           
        <input type="submit" value="Next Question"    />
        <input type="submit" value="Submit" />
    <input type="submit" value="Quit Exam" />
    }
    }
    }
(model class)
C#
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// 
//------------------------------------------------------------------------------

namespace OAGES5
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class QuestionareLevel1Entities1 : DbContext
    {
        public QuestionareLevel1Entities1()
            : base("name=QuestionareLevel1Entities1")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public DbSet<type1> Type1 { get; set; }
        public DbSet<type2> Type2 { get; set; }
        public DbSet<type3> Type3 { get; set; }
    }
}

( model class)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OAGES5.Models
{
    public class Level1
    {
        public string SelectedAnswer { get; set; }
        public List<type1> T1
        {
            get
            {
                QuestionareLevel1Entities1 QL1E1 = new QuestionareLevel1Entities1();

                return QL1E1.Type1.ToList();
            }
        }
    }
}
Posted
Updated 19-Mar-15 2:11am
v2
Comments
Ram Kawale 19-Mar-15 8:05am    
Do I missing Something in linq Query?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900