Asp.net mvc 3 如何将列绑定到DropDownList并在MVC3中选择名称?
现在我展示了项目表,其中他有一个Assing链接,当Assignwork点击Assign链接时,我将在下拉列表中向他展示所有项目名称 模型代码Asp.net mvc 3 如何将列绑定到DropDownList并在MVC3中选择名称?,asp.net-mvc-3,c#-4.0,Asp.net Mvc 3,C# 4.0,现在我展示了项目表,其中他有一个Assing链接,当Assignwork点击Assign链接时,我将在下拉列表中向他展示所有项目名称 模型代码 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用系统数据; 使用System.Data.SqlClient; 使用System.Data.Entity; 命名空间Gridview_BugTracker.Models { 公共类BugTracker\u DataHelpe
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用系统数据;
使用System.Data.SqlClient;
使用System.Data.Entity;
命名空间Gridview_BugTracker.Models
{
公共类BugTracker\u DataHelper
{
公共静态列表GetList{get;set;}
公共int ProjectId{get;set;}
公共字符串projectName{get;set;}
公共字符串说明{get;set;}
公共字符串状态{get;set;}
public int EmployeId{get;set;}
公共字符串EmployeName{get;set;}
}
}
控制器代码
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用系统数据;
使用Gridview_BugTracker.Models;
使用System.Data.SqlClient;
使用System.Data.Entity;
命名空间Gridview_BugTracker.Controllers
{
公共类项目控制器:控制器
{
//
//获取:/Projects/
///
///
///
///
公共行动结果索引()
{
var bugedlist=GetList();
返回视图(bugedlist);
}
///
///Gridview中的绑定数据
///
///
公共列表GetList()
{
var modelList=新列表();
使用(SqlConnection conn=newsqlconnection(@“integratedsecurity=SSPI;persistedsecurity Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS”))
{
conn.Open();
SqlCommand dCmd=新的SqlCommand(“从项目中选择*”,conn);
SqlDataAdapter da=新的SqlDataAdapter(dCmd);
数据集ds=新数据集();
da.填充(ds);
康涅狄格州关闭();
对于(int i=0;i
指数
指数
项目名称
地位
|
|
辅助视图代码
转让项目
项目
项目名称:
我出错了
“/”应用程序中出现服务器错误。
传递到字典的模型项的类型为“System.Collections.Generic.List`1[Gridview\u BugTracker.Models.BugTracker\u DataHelper]”,但此字典需要类型为“Gridview\u BugTracker.Models.BugTracker\u DataHelper”的模型项
当我点击aspx页面中的Assingn按钮时,它应该转到我控制器中的Assingn方法所有都是列显示数据,但未获得设计页面……我在这里做错了什么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity;
namespace Gridview_BugTracker.Models
{
public class BugTracker_DataHelper
{
public static List<BugTracker_DataHelper> GetList{get;set;}
public int ProjectId { get; set; }
public string projectName { get; set; }
public string Description { get; set; }
public string status { get; set; }
public int EmployeId { get; set; }
public string EmployeName { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Gridview_BugTracker.Models;
using System.Data.SqlClient;
using System.Data.Entity;
namespace Gridview_BugTracker.Controllers
{
public class ProjectsController : Controller
{
//
// GET: /Projects/
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
var bugedlist = GetList();
return View(bugedlist);
}
/// <summary>
/// The Bind data in Gridview
/// </summary>
/// <returns></returns>
public List<BugTracker_DataHelper> GetList()
{
var modelList = new List<BugTracker_DataHelper>();
using (SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
{
conn.Open();
SqlCommand dCmd = new SqlCommand("Select * from Projects", conn);
SqlDataAdapter da = new SqlDataAdapter(dCmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
var model = new BugTracker_DataHelper();
model.ProjectId = Convert.ToInt16(ds.Tables[0].Rows[i]["ProjectId"]);
model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
model.status = ds.Tables[0].Rows[i]["Status"].ToString();
modelList.Add(model);
}
}
return modelList;
}
/// <summary>
/// Assign The Project Display
/// </summary>
/// <returns></returns>
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AssignProject()
{
var bugedlist = Assinedwrok();
return View(bugedlist);
}
public List<BugTracker_DataHelper> Assinedwrok()
{
var modelList = new List<BugTracker_DataHelper>();
using(
SqlConnection assignedconn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
{
{
assignedconn.Open();
SqlCommand Passnedcmd = new SqlCommand("select projectName from Projects", assignedconn);
SqlDataAdapter da = new SqlDataAdapter(Passnedcmd);
DataSet ds = new DataSet();
da.Fill(ds);
SqlDataReader adr = Passnedcmd.ExecuteReader();
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
var model = new BugTracker_DataHelper();
model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
modelList.Add(model);
}
}
return modelList;
}
}
/// <summary>
/// The Assigned The Work to Resources
/// </summary>
/// <returns></returns>
public ActionResult AssignedWork()
{
return View();
}
}
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<h2>Index</h2>
<p>
<%: Html.ActionLink("Create New", "Create") %>
<%:Html.ActionLink("Assign", "AssignProject")%>
</p>
<table>
<tr>
<th>
ProjectName
</th>
<th>
Status
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%:Html.LabelForModel(item.projectName) %>
</td>
<td>
<%:Html.LabelForModel(item.status) %>
</td>
<td>
<%: Html.ActionLink("Edit", "Edit", new {id = item.ProjectId})%> |
<%: Html.ActionLink("Details", "Details", new { id = item.ProjectId })%> |
<%: Html.ActionLink("Delete", "Delete", new { id = item.ProjectId })%>
</td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Gridview_BugTracker.Models.BugTracker_DataHelper>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>AssignProject</title>
</head>
<body>
<div>
<% using (Html.BeginForm())
{ %>
<%:Html.ValidationSummary(true)%>
<fieldset>
<legend>Projects</legend>
<label for="ProjectName">ProjectName:</label>
<%: Html.DropDownList("SelectItem",Model.projectName) %>
</fieldset>
<%} %>
</div>
</body>
</html>