using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace PollingWebsite.Models
{
public class Poll // this is a poll object with different attributes , this is a model , a class , a object
{
public Poll()
{
Options1=new List<Option>();// options created into an empty list
}
public Poll(DateTime date, DateTime edate, int id, string question,List <Option> options1) // create an instance and giving it to me to save
{
Date = date;
Edate = edate;
ID = id;
Question = question;
Options1 = options1;
}
[Display(Name = "Date")]
public DateTime Date{get; set;}
[Display(Name = "Edate")]
public DateTime Edate{get; set;}
[Display(Name = "Question")]
public string Question{ get; set;}
[Display(Name = "id")]
public int ID { get; set; }
[Display(Name ="Options1")]
public List<Option> Options1 { get; set; }
}
public class Option// this is where the options are defined for the poll, this is the option Class
{
public Option(int pollid, string text,int optionid) // options are a class of its own
{
Pollid = pollid;
Text = text;
Optionid = optionid;
}
public Option()
{
// // TODO: Complete member initialization
}
public int Pollid { get; set; }
public string Text { get; set; }
public int Optionid { get; set; }
}
现在这里是我的控制器,我无法让它工作,Create Poll控件就是你可以看到我想在每次从控制器读取Poll对象时添加一个Options List,如果需要我可以发布我的查看但截至目前我不知道如何在此控制器中添加选项列表。
namespace PollingWebsite.Controllers
{
public class PollController : Controller // poll of poll/create
{
//
// GET: /Poll/Create
public ActionResult Create() // crate a new poll
{
Poll model1= new Poll();// instantiate class into instance of it
model1.Options1.Add(new Option());// never got created options1
model1.Options1.Add(new Option());
return View(model1);
}
//
// POST: /Poll/Create
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Create(Poll collection) // resolve add poll into here, in memory but
{
if (ModelState.IsValid)
{
//try
//{
// TODO: Add insert logic here
//var poll = PollDataSource.Instance().Get(collection.ID); // how will I call the database now?, just creating
//if (poll != null)
//{
// ModelState.AddModelError("Poll already exists", "That Poll has already been created");
//}
//else
{
Poll numberid= PollDataSource.Instance().Add(collection.Date, collection.Edate,collection.ID, collection.Question); //calling code that calls the source that calls the database
foreach (Poll model in numberid)
{
PollDataSource.Instance().Add(collection.ID,collection.Options1);
}
// get questions somehow foreach (var value in numberid)
//{
// PollDataSource.
//}
// Authentication.Login(model.Username, model.Password);
return RedirectToAction("Index", "Home");
}
//return RedirectToAction("Index");
//}
//catch
//{
// return View();
//}
}
return View(collection);
}
}
}