我正在研究包含ViewModel和Controller的MVC.Net項目。爲什麼我不使用Model,因爲我使用ADO.Net而不是實體框架來連接數據庫。在C#中不包含System.Web.MVC的情況下添加SelectListItem()類
爲此,我在不同的項目中將ViewModels與控制器分開。這裏是我的問題, 我利用using System.Web.MVC
中的SelectListItem類,但只安裝在我的Controller項目中。控制器代碼片段的
這裏例如:
public ActionResult Index()
{
ViewBag.ddlistname = new List<SelectListItem> {
new SelectListItem { Text = "Anthony", Value = "1" },
new SelectListItem { Text = "Miranda", Value = "2" },
new SelectListItem { Text = "Jhon", Value = "3" }
};
ViewBag.ddlistpos = new List<SelectListItem> {
new SelectListItem { Text = "Store Clerk", Value = "A" },
new SelectListItem { Text = "Waiter", Value = "B" },
new SelectListItem { Text = "Cook", Value = "C" }
};
var result = new CRViewModel();
return View(result);
}
查看:
@Html.DropDownListFor(model => model.EmpName,(IEnumerable<SelectListItem>)ViewBag.ddlistname)
@Html.DropDownListFor(model => model.EmpPost,(IEnumerable<SelectListItem>)ViewBag.ddlistpos)
我知道這是一個不好的做法使用ViewBag這就造成了內部控制很多ViewBag填補List<SelectListItem>()
,不在View中提到鑄造ViewBag。
這就是我希望我的ViewModels這樣
public class CRViewModels
{
public string EmpName { get; set; }
public string EmpPos { get; set; }
public List<SelectListItem> EmpList = new List<SelectListItem>
{
new SelectListItem { Text = "Anthony", Value = "1" },
new SelectListItem { Text = "Miranda", Value = "2" },
new SelectListItem { Text = "Jhon", Value = "3" }
};
public List<SelectListItem> PosList = new List<SelectListItem>
{
new SelectListItem { Text = "Store Clerk", Value = "A" },
new SelectListItem { Text = "Waiter", Value = "B" },
new SelectListItem { Text = "Cook", Value = "C" }
};
}
所以在控制器我可以寫
public ActionResult Index()
{
var result = new CRViewModel();
return View(result);
}
,並考慮:
@Html.DropDownListFor(model => model.EmpName,Model.EmpList)
@Html.DropDownListFor(model => model.EmpPost,Model.PosList)
,但我不能,因爲在我的ViewModels(VM)項目,我沒有命名空間System.Web.MVC
。如果我添加dll,並且僅在使用SelectListItem類時將System.Web.MVC
名稱空間添加到我的VM項目,那就太浪費了。
如果可能的話,如何添加SelectListItem類而不添加整個名稱空間?
編輯: 爲您的要求,我添加了我的真實代碼示例 這是我的代碼如何工作。
在DAL類:
public List<SelectListItem> getcustlist()
{
string SQL = "select cust_name, cust_id from tbl_cust";
List <SelectListItem> result = getdropdownfromSQL(SQL, "cust_name", "cust_id ");
return result;
}
public List<SelectListItem> getpostype()
{
string SQL = "select pos_name, pos_code from tbl_pos";
List<SelectListItem> result = getdropdownfromSQL(SQL, "pos_name", "pos_code");
return result;
}
public List<SelectListItem> getdropdownfromSQL(string SQL, string Text, string Value)
{
string Conn = GetConn();//function to get connection string
List<SelectListItem> result = new List<SelectListItem>();
string errmsg;
DataTable fetch = getdt(SQL, Conn, out errmsg);
if (fetch == null)
return result;
int rowaffect = fetch.Rows.Count;
for (int i = 0; i < rowaffect; i++)
{
result.Add(new SelectListItem
{
Text = fetch.Rows[i][Text].ToString(),
Value = fetch.Rows[i][Value].ToString()
});
}
return result;
}
public DataTable getdt(string SQL, string strconn, out string errormsg)
{
errormsg = "";
DataTable dt = new DataTable();
try
{
using (SqlConnection conn = new SqlConnection(strconn))
{
using (SqlDataAdapter da = new SqlDataAdapter(SQL, conn))
{
conn.Open();
da.Fill(dt);
}
}
}
catch (Exception ex)
{
errormsg = ex.Message;
return null;
}
return dt;
}
在控制器:
private DAL func = new DAL();
public ActionResult Index()
{
ViewBag.ddlistname = func.getcustlist();
ViewBag.ddlistpos = func.getpostype();
var result = new CRViewModel();
return View(result);
}
您需要添加的命名空間。這樣做是有道理的。它是一個視圖模型,特定於'System.Web.MVC'中的視圖。而且命名空間正在您的應用程序中使用,所以它沒有任何區別(不清楚爲什麼您認爲浪費_) –
因爲我使用Visual Studio中的Nuget包來在需要Internet訪問的項目中添加dll。在我的工作場所,上網受到限制。我只能在這裏訪問Intranet。雖然可以請求臨時訪問,但這裏有點複雜,所以需要做很多工作。 – KokoriNut
您使用MVC所以''.dll'已經安裝在您的計算機上 - 您不需要使用Nuget –