在C#中添加不包含System.Web.MVC的SelectListItem()类 [英] Adding SelectListItem() class without include System.Web.MVC in C#
问题描述
我正在处理包含ViewModels和Controller的MVC.Net项目.为什么我不使用模型,因为我使用ADO.Net代替Entity Framework进行数据库连接.
I am working on MVC.Net project which contains ViewModels and Controller. For why I don't use Model, because I use ADO.Net instead of Entity Framework for my DB connection.
为此,我在不同项目中将ViewModels与Controller分开.这是我的问题我在中使用System.Web.MVC
利用SelectListItem类,但只能将其安装在我的Controller项目中.
To do so, I separate ViewModels with Controller in different project. Here is my problem,
I utilize SelectListItem class in using System.Web.MVC
, but only install it in my Controller project.
以下是控制器代码段的示例:
Here example of Controller code snippet:
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填充 List< SelectListItem>()
是一种不好的做法,这会导致Controller内部出现大量ViewBag,更不用说在View中投射ViewBag了.
I know it's a bad practice to fill List<SelectListItem>()
using ViewBag which resulted in a lot of ViewBag inside Controller, not to mention casting ViewBag in View.
为什么我要这样的ViewModels
Which why I want my ViewModels like this
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" }
};
}
所以在Controller中我可以写
So in Controller I could write
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
.如果我仅在使用SelectListItem类的情况下向我的VM项目中添加dll并包含 System.Web.MVC
命名空间,那将是一种浪费.
but I can't because in my ViewModels(VM) project, I don't have namespace System.Web.MVC
. It's rather a waste if I add dll and include System.Web.MVC
namespace to my VM project only for using the SelectListItem class.
如果可能,如何在不添加整个命名空间的情况下添加SelectListItem类?
If it is possible, How could I add SelectListItem class without adding whole namespace?
根据您的要求,我添加了我的真实代码示例这就是我的代码的工作方式.
Edit : for your request, I add my real code example This is how my code works.
在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);
}
推荐答案
不添加名称空间就无法实现此行为,因为它存储了有关此类的信息.
You can not achieve this behaviour without adding the namespace, as it stored info about this class.
但是,您可以创建自己的选择项类,并创建用于将您的类项转换为 SelectListItem
的扩展方法,如下所示:
However, you can create your own select item class and create an extension method for converting items of your class to SelectListItem
as following:
public class SimpleItem
{
public string Text { get; set; }
public string Value { get; set; }
}
SimpleItem
应该存储在可以访问 ViewModel
和 View
的程序集中.
SimpleItem
should be stored in assembly to which ViewModel
s and View
s have access.
然后在MVC项目中创建一个扩展方法:
And in the MVC project create an extension method:
public static class HtmlExtensions
{
public static MvcHtmlString LocalDropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expr,
IEnumerable<SimpleItem> items)
{
return helper.DropDownListFor(expr,
items.Select(x => new SelectListItem { Text = x.Text, Value = x.Value } ));
}
}
您应包括 System.Web.Mvc.Html
,以启用从 helper
调用 DropDownListFor
的方法.
You should include System.Web.Mvc.Html
for enabling DropDownListFor
method call from helper
.
如果这是您的第一个 HtmlHelper
扩展名,最好将此类的 namespace
包含在web.config页面中.否则,您将需要手动将其包括在页面上:
If it is your first HtmlHelper
extension, would be better to include namespace
of this class into page web.config. Or you will be required to include it on page manually:
@using YourProject.RequiredNamespace
毕竟,您可以在页面上简单地调用它:
After all you could simple call it on page:
@Html.LocalDropDownListFor(model => model.EmpName, Model.EmpList)
这篇关于在C#中添加不包含System.Web.MVC的SelectListItem()类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!