我有一个模特:
public class DbUserRole
{
public int UserRoleId { get; set; }
public string UserRole { get; set; }
}
public class DbUserRoles
{
public List<DbUserRole> GetRoles()
{
BugnetReports RoleDropDown = new BugnetReports();
List<DbUserRole> Roles = new List<DbUserRole>();
DataSet table = RoleDropDown.userRoleDropDown();
foreach (DataRow item in table.Tables[0].Rows)
{
DbUserRole ur = new DbUserRole();
ur.UserRole = Convert.ToString(item["UserRoleName"]);
ur.UserRoleId = Convert.ToInt32(item["UserRoleID"]);
Roles.Add(ur);
}
return Roles;
}
}
Run Code Online (Sandbox Code Playgroud)
这是加载视图的Controller:
//
// GET: /Admin/AddNewUser
public ActionResult AddNewUser()
{
DbUserRoles Roles = new DbUserRoles();
return …Run Code Online (Sandbox Code Playgroud) 我可以使用一些帮助实现@ Html.DropDownListFor.我的目标是按类别过滤产品列表.
此代码将显示一个列表框:
@model IEnumerable<Sample.Models.Product>
@{
List<Sample.Models.Category> list = ViewBag.Categories;
var items = new SelectList(list, "CategoryID", "CategoryName");
}
@Html.DropDownList("CategoryID", items)
Run Code Online (Sandbox Code Playgroud)
但我@Html.DropDownListFor上班有困难:
@model IEnumerable<Sample.Models.Product>
@{
List<Sample.Models.Category> list = ViewBag.Categories;
var items = new SelectList(list, "CategoryID", "CategoryName");
}
@Html.DropDownListFor(???, @items)
Run Code Online (Sandbox Code Playgroud)
我可以使用一些帮助构建Linq部分@Html.DropDownListFor.这是模型:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
public string QuantityPerUnit { get; set; }
public Decimal? UnitPrice { get; …Run Code Online (Sandbox Code Playgroud) 好的,所以我是整个MVC世界的新手,但它似乎是完成任务的一种非常好的方式,我试图让它在这里工作.
问题是:我无法从我的SQL数据库中的表中获取数据到我的注册页面上的简单下拉表单.
我不知道在哪里放东西,在哪里编写代码打开表,选择ID,在哪里放置response.write以及如何将其发送到视图?
我的模型是这样的:
public class users
{
public string name {get; set;}
public int user_id {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器是这样的:
[HttpGet]
public ActionResult ListUser()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
我的观点是这样的:
@model Community.Models.users
Run Code Online (Sandbox Code Playgroud)
我已经用Google搜索了2天,在youtube上观看了几个视频但没有用,我找不到它.请知道这里有一些知识的人吗?请指出一些很好的教程和/或论坛,我可以浏览更多我可能有的问题
这个项目仍然没有运气..
我正在创建一个表单并在该表单中,我想要一个db-loop(IEnumerable)..但是当前的模型不是IEnumerable.我几乎陷入困境,观看了一堆教程,他们都只列出了一个连接,如果我想要两个模型怎么办?
这是我的控制器,我知道你必须将一个列表传递给视图,对吧?
public ActionResult Registration()
{
return View(db.users.ToList());
}
Run Code Online (Sandbox Code Playgroud)
如何在IEnumerable模型中查看我的视图中的列表?
@neoistheone,你的例子对我没什么帮助,我的DB打开像这样:
private DataBaseContext db = new DataBaseContext();
Run Code Online (Sandbox Code Playgroud)
我不知道如何,但它打开了连接.我已经尝试了这么多个小时,它只是愚蠢,没有睡太久!
我习惯编写ASP-Classic fyi,这是我第一次认真尝试升级我对编程最新语言和OOP的知识.
在我的剃刀中,我正在生成这样的下拉列表.
@{
var listItems = new List<ListItem>
{
new ListItem { Text = "Home To School", Value = "0" },
new ListItem { Text = "School To Home", Value = "1" }
};
}
@Html.DropDownList("Direction", new SelectList(listItems),new {onchange = "getAlldata()"})
Run Code Online (Sandbox Code Playgroud)
由此生成的HTML就是这样
<select id="Direction" name="Direction" onchange="getAlldata()">
<option>Home To School</option>
<option>School To Home</option>
</select>
Run Code Online (Sandbox Code Playgroud)
但我想生成像这样的HTML
<select id="Direction" name="Direction" onchange="getAlldata()">
<option value="0">Home To School</option>
<option value="1">School To Home</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点.