0 Replies - 668 Views - Last Post: 23 November 2014 - 02:00 AM

#1 vasilen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-November 14

Need help in changing a view

Posted 23 November 2014 - 02:00 AM

I want to create a survey form with questions from db (about 90 questions)

I got these tables

Survey: SurveyId, UserId, QuestionId, Answer, Comment
User: UserId, Name
Question: QuestionId, QuestionText

When i created no labels/textboxes appear

Survey Controller :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OrganizationASEM.Models;

namespace OrganizationASEM.Controllers
{
    public class SurveysController : Controller
    {
        private OrganizationASEMEntities1 db = new OrganizationASEMEntities1();

        // GET: Surveys
        public ActionResult Index()
        {
            var surveys = db.Surveys.Include(s => s.Question).Include(s => s.User);
            return View(surveys.ToList());
        }

        // GET: Surveys/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Survey survey = db.Surveys.Find(id);
            if (survey == null)
            {
                return HttpNotFound();
            }
            return View(survey);
        }

        // GET: Surveys/Create
        public ActionResult Create()
        {
           /* ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText");
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Name");
                return View();*/
            var surveys = db.Surveys.Include(s => s.Question).Include(s => s.User);
            return View(surveys.ToList());
            
        }

        // POST: Surveys/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "SurveyId,UserId,QuestionId,Answer,Comment")] Survey survey)
        {
            if (ModelState.IsValid)
            {
                db.Surveys.Add(survey);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText", survey.QuestionId);
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
            return View(survey);
        }

        // GET: Surveys/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Survey survey = db.Surveys.Find(id);
            if (survey == null)
            {
                return HttpNotFound();
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText", survey.QuestionId);
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
            return View(survey);
        }

        // POST: Surveys/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "SurveyId,UserId,QuestionId,Answer,Comment")] Survey survey)
        {
            if (ModelState.IsValid)
            {
                db.Entry(survey).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.QuestionId = new SelectList(db.Questions, "QuestionId", "QuestionText", survey.QuestionId);
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Name", survey.UserId);
            return View(survey);
        }

        // GET: Surveys/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Survey survey = db.Surveys.Find(id);
            if (survey == null)
            {
                return HttpNotFound();
            }
            return View(survey);
        }

        // POST: Surveys/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Survey survey = db.Surveys.Find(id);
            db.Surveys.Remove(survey);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}




Survey View Create
@model IEnumerable<OrganizationASEM.Models.Survey>

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    

        <h4>Survey</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    foreach (var item in Model)
    {  @Html.LabelFor(modelitem => item.User)
           @Html.LabelFor(modelitem => item.QuestionId)
  
    @Html.TextBoxFor(modelitem => item.Answer)
        }

}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>




Is This A Good Question/Topic? 0
  • +

Page 1 of 1