Controller-
namespace TaskList.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
private TaskListDataContext DB = new TaskListDataContext();
//Task tasks = new Task();
//Display a list of Tasks
public ActionResult Index()
{
var tasks = from t in DB.Tasks
orderby t.EntryDate descending
select new TaskList.Models.Task()
{
Id = t.Id,
Task1 = t.Task1,
IsCompleted = t.IsCompleted,
EntryDate = t.EntryDate
};
return View(tasks);
}
//Display a form for creating a new Task
public ActionResult Create()
{
return View();
}
//Adding a new task to the database
public ActionResult CreateNew(string tasktext)
{
//Add the new task to the database
Task newTask = new Task();
newTask.Task1 = tasktext;
newTask.IsCompleted = false;
newTask.EntryDate = DateTime.Now;
DB.Tasks.InsertOnSubmit(newTask);
DB.SubmitChanges();
return RedirectToAction("Index");
}
//Mark a task as complete
public ActionResult Complete()
{
//Database Logic
return RedirectToAction("Index");
}
}
}
View:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<TaskList.Models.Task>>"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<div>
<h1>My Tasks</h1>
<ul>
<% foreach (TaskList.Models.Task task in ViewData.Model)
{%>
<li>
<% task.Task1.ToString(); %>
</li>
<% } %>
</ul>
<br /> <br />
<a href='<%=Url.Action("Create")%>/filename'>Add my Tasks</a>
</div>
</body>
</html>
Model is made using Linq Class-
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Tasks")]
public partial class Task : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _Id;
private string _Task1;
private bool _IsCompleted;
private System.DateTime _EntryDate;
}
When I am trying to run the Application it is showing me an error
"Explicit construction of entity type 'TaskList.Models.Task' in query is not allowed."
in the foreach loop of view.
Actually I am trying to create a C# veriosn of the Application built in VB over here.
http://www.asp.net/mvc/videos/mvc-2/how-do-i/creating-a-tasklist-application-with-aspnet-mvc
您可能需要转换为类似的列表,
var tasks = (from t in DB.Tasks orderby t.EntryDate descending select new TaskList.Models.Task() { Id = t.Id, Task1 = t.Task1, IsCompleted = t.IsCompleted, EntryDate = t.EntryDate }).ToList();
As the error message suggests, you can't create mapped entity in query statement. You can either select existing entity :
var tasks = from t in DB.Tasks
orderby t.EntryDate descending
select t;
, or create list of new entities outside query :
var taskEntities = from t in DB.Tasks
orderby t.EntryDate descending
select t;
var tasks = new List<TaskList.Models.Task>();
foreach(var taskEntity in taskEntities)
{
tasks.Add(new TaskList.Models.Task()
{
Id = taskEntity.Id,
Task1 = taskEntity.Task1,
IsCompleted = taskEntity.IsCompleted,
EntryDate = taskEntity.EntryDate
});
}
return View(tasks);
, depending on your requirement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.