I have a requirement to develop a short survey using MVC.
My database has a table containing questions, and another containing possible answers, which are to be displayed as radio buttons for multiple choice answers.
For example, a question with it's answers might look like this
Was this delivered on time?
Yes, No, Not Sure
So I set up a model for the questions which looks like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace SatisfactionSurvey.Models
{
public class ITSLA_RFC_Questionnaire_Questions
{
[Key]
public int ID { get; set; }
public string QuestionText { get; set; }
[StringLength(20)]
public string QuestionCode { get; set; }
public virtual ICollection<ITSLA_RFC_Questionnaire_Params> Params { get; set; }
}
}
And another for the answers which looks like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace SatisfactionSurvey.Models
{
public class ITSLA_RFC_Questionnaire_Params
{
[Key]
public int ID { get; set; }
public int QuestionID { get; set; }
public int Response { get; set; }
[StringLength(50)]
public string ResponseText { get; set; }
public int Points { get; set; }
[StringLength(20)]
public string QuestionCode { get; set; }
}
}
I then created a controller looking something like this...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SatisfactionSurvey.Models;
namespace SatisfactionSurvey.Controllers
{
public class HomeController : Controller
{
private SatisfactionSurveyDBContext db = new SatisfactionSurveyDBContext();
public ActionResult Index(string TicketNo)
{
ViewBag.Message = "Please answer the following questions.";
if (!string.IsNullOrEmpty(TicketNo))
{
return View();
}
else
{
return View(db.Questions.ToList());
}
}
When I try to run this, I get an error appearing telling me I have an invalid column name. My question is, am I misunderstanding how partial views work? Or is there a better way to do this. I'm new to MVC4, so all advice, or suggestions on reading matter
appreciated!
Thanks Eric, that solved part of the problem - I'd been staring at the model so long I couldn't see it!
But I'm now getting another error - if anyone has any hints I'd appreciate it
The model item passed into the dictionary is of type 'System.Linq.Enumerable+WhereSelectEnumerableIterator`2[SatisfactionSurvey.Models.ITSLA_RFC_Questionnaire_Params,System.Boolean]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SatisfactionSurvey.Models.ITSLA_RFC_Questionnaire_Params]'.
ukeruth
Member
24 Points
25 Posts
Passing data to a partial view
Dec 21, 2012 01:33 PM|LINK
I have a requirement to develop a short survey using MVC.
My database has a table containing questions, and another containing possible answers, which are to be displayed as radio buttons for multiple choice answers.
For example, a question with it's answers might look like this
Was this delivered on time?
Yes, No, Not Sure
So I set up a model for the questions which looks like
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace SatisfactionSurvey.Models { public class ITSLA_RFC_Questionnaire_Questions { [Key] public int ID { get; set; } public string QuestionText { get; set; } [StringLength(20)] public string QuestionCode { get; set; } public virtual ICollection<ITSLA_RFC_Questionnaire_Params> Params { get; set; } } }And another for the answers which looks like
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace SatisfactionSurvey.Models { public class ITSLA_RFC_Questionnaire_Params { [Key] public int ID { get; set; } public int QuestionID { get; set; } public int Response { get; set; } [StringLength(50)] public string ResponseText { get; set; } public int Points { get; set; } [StringLength(20)] public string QuestionCode { get; set; } } }I then created a controller looking something like this...
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SatisfactionSurvey.Models; namespace SatisfactionSurvey.Controllers { public class HomeController : Controller { private SatisfactionSurveyDBContext db = new SatisfactionSurveyDBContext(); public ActionResult Index(string TicketNo) { ViewBag.Message = "Please answer the following questions."; if (!string.IsNullOrEmpty(TicketNo)) { return View(); } else { return View(db.Questions.ToList()); } }and a view
model IEnumerable<SatisfactionSurvey.Models.ITSLA_RFC_Questionnaire_Questions> @{ ViewBag.Title = "Satisfaction Survey"; ViewBag.Space = " "; } @section featured { <section class="featured"> <div class="content-wrapper"> <hgroup class="title"> <h1>@ViewBag.Title.</h1> <h2>@ViewBag.Message</h2> </hgroup> <p> </p> </div> </section> } <form action="home/submit" method="get"> <ol> @foreach (var item in Model) { <li>@Html.DisplayFor((modelItem => item.QuestionText)) <p> @Html.Partial("_partialQuestion", item.Params.Select(s => s.QuestionID == item.ID)); </li> } </ol> <p>@Html.Label("Comments") @Html.TextArea("Comments") </p> <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" /> </form>with a partial view
@model IEnumerable<SatisfactionSurvey.Models.ITSLA_RFC_Questionnaire_Params> @{ ViewBag.Space = " "; } @foreach (var resp in Model ) { @Html.RadioButton(name: resp.QuestionCode.ToString(), value: resp.ID.ToString(), isChecked: false, htmlAttributes: new { GroupName = resp.QuestionID, @class = "required", data_val = "true", data_val_required = "*" }); @ViewBag.Space @resp.ResponseText @ViewBag.Space }When I try to run this, I get an error appearing telling me I have an invalid column name. My question is, am I misunderstanding how partial views work? Or is there a better way to do this. I'm new to MVC4, so all advice, or suggestions on reading matter appreciated!
eric2820
Contributor
2777 Points
1161 Posts
Re: Passing data to a partial view
Dec 21, 2012 02:23 PM|LINK
If you getting an invalid column name, that is a missmatch between your Entity class and the database and has nothing to do with partial view.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ukeruth
Member
24 Points
25 Posts
Re: Passing data to a partial view
Dec 21, 2012 02:38 PM|LINK
Thanks Eric, that solved part of the problem - I'd been staring at the model so long I couldn't see it!
But I'm now getting another error - if anyone has any hints I'd appreciate it
The model item passed into the dictionary is of type 'System.Linq.Enumerable+WhereSelectEnumerableIterator`2[SatisfactionSurvey.Models.ITSLA_RFC_Questionnaire_Params,System.Boolean]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[SatisfactionSurvey.Models.ITSLA_RFC_Questionnaire_Params]'.
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: Passing data to a partial view
Dec 21, 2012 02:54 PM|LINK
return a query, you probably want something like:
item.Params.Select(s => s.QuestionID == item.ID).ToList()note: its really a bad practice to pass dbcontexts to the view. the sql query should be done in the controller.
ukeruth
Member
24 Points
25 Posts
Re: Passing data to a partial view
Dec 21, 2012 03:07 PM|LINK
I'm just about there now, thanks for the help!
Using just
I can see values going into my partial view, I just need to get it to display them the way I want it to ;)