Here is my class :
public class QuestionClass
{
public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext();
public class Tabelfields : Question
{
//public int QuestionID { get; set; }
//public string Email { get; set; }
//public string QuestionName { get; set; }
//public string Option1 { get; set; }
//public string Option2 { get; set; }
//public string Option3 { get; set; }
//public string Option4 { get; set; }
public string SelectedOption { get; set; }
}
public static List<Question> getallQuestion(string email)
{
var list = (from q in context.Questions where q.Email==@email select q).ToList();
return list.ToList();
}
}
My view :
@model IEnumerable SQLOperation.Models.QuestionClass.Tabelfields
<p> Question</p>
@foreach (var item in Model)
{
using (Html.BeginForm("Question", "home", new {email=item.Email,item}))
{
@Html.DisplayFor(modelItem => item.QuestionName)
<br /><br />
if (item.Option1 != "")
{
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option1, item)
@Html.DisplayFor(modelItem => item.Option1)
<br /><br />
}
if (item.Option2 != "")
{
@Html.RadioButtonFor(modelItem => item.SelectedOption, item.Option2)
@Html.DisplayFor(modelItem => item.Option2)
<br /><br />
}
if (item.Option3 != "")
{
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option3)
@Html.DisplayFor(modelItem => item.Option3)
<br /><br />
}
if (item.Option4 != "")
{
@Html.RadioButtonFor(modelItem =>item.SelectedOption, item.Option4)
@Html.DisplayFor(modelItem => item.Option4)
<br /><br />
}
i = (Int16)i + 1;
if (Model.Count() == i)
{
<input name="btnsumbit" type="submit" value="Submit Feedback"
style="font-family:Segoe UI Light;font-size:medium;"/>
}
}
}
Here Is my controller
public ActionResult Question(string email)
{
return View(QuestionClass.getallQuestion(email));
}
[HttpPost, ActionName("Question")]
public void Question(string Email,List<QuestionClass.Tabelfields> q)
{
}
In class i.e. "Tabelfields" I create new property i.e. SelectedOption. I inherite the base class i.e. Question. Where Question is a table in Sql server Database.
I create strongely type view by using this
@model IEnumerable SQLOperation.Models.Question
If I change strongely type view as
@model IEnumerable SQLOperation.Models.QuestionClass.Tabelfields
I get this error
"The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[SQLOperation.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[SQLOperation.Models.QuestionClass+Tabelfields]'."
Why should I get this error and how can I solve this ?
Thank you,
ajay
You need to change:
to this:
That should fix the error.
Hope it helps.