Я занимаюсь разработкой приложения в MVC3.По сути, это форум вопросов, где на странице «Вопрос» пользователь увидит все вопросы в виде ссылки и поле для комментария в конце. И когда он щелкнет по ссылке вопроса, он будет перемещен на страницу ответов. Теперь страница ответов использует другой класс модели.и их я не могу получить доступ к своим данным класса ответа
, что я обнаружил, что у каждого контроллера есть представление и модель, но я хочу, чтобы у моего представления был доступ к нескольким моделям, и я не знаю, как это сделать ..
Я попробовал это:
public class MainClass
{
public Question_Page question{ get; set; }
public Answer_Page answer { get; set; }
}
public class Question_Page
{
public virtual int Id { get; set; }
public virtual string Question_name { get; set; }
public virtual DateTime Created_Date { get; set; }
public virtual DateTime Modified_Date { get; set; }
public virtual int Created_By { get; set; }
public virtual int Modified_By { get; set; }
public virtual char Deleted { get; set; }
public Question_Page()
{
this.Deleted='F';
}
}
public class Answer_Page
{
public virtual int Id { get; set; }
public virtual string Answer { get; set; }
public virtual DateTime Created_Date { get; set; }
public virtual DateTime Modified_Date { get; set; }
public virtual int Created_By { get; set; }
public virtual int Modified_By { get; set; }
public virtual char Deleted { get; set; }
public virtual Question_Page Question { get; set; }
public Answer_Page()
{
this.Deleted='F';
}
}
Теперь внутри представления списка вопросов, где я отображаю список вопросов:
@model IEnumerable<Core.Model.MainPage>
@{
ViewBag.Title = "Index";
}
<style type="text/css">
ul
{
list-style-type: none;
}
</style>
<h2>All Questions</h2>
<hr />
@using (Html.BeginForm("Index","Question",FormMethod.Post))
{
@Html.ValidationSummary(true)
<ul>
@foreach (var item in Model)
{
<li>@Html.ActionLink(item.Question_name, "answer", new { Qry = item.Id })</li>
}
</ul>
<label for="PostyourQuestion:">Post your Question:</label><br /><br />
@Html.TextArea("textID")
<br />
<input type="submit"/>
}
после этого я получаюстрока ошибки: @foreach (элемент var в Model)
Это мой контроллер:
public ActionResult Index()
{
return View(new StudentService().GetAllStudents());
}
[HttpPost]
public ActionResult Index(Question_Page question,string textID)
{
question.Question_name = textID;
question.Created_Date = DateTime.Now;
question.Modified_Date = DateTime.Now;
question.Created_By = 101;
question.Modified_By = 101;
new StudentService().SaveOrUpdateStudent(question);
return View(new StudentService().GetAllStudents());
}
StudentService - это класс, в котором я определил методы GetAllStudents (), SaveOrUpdateStudent ()
Пожалуйста, помогите мне