问题
Short about the website: Website where users can log in to see the logs in our database and where admins can add new users and change the roles of the users in the database.
On my "Register new users page" I add the users to a specific 'CustomerID' and I want the admin to only be able to select the 'CustomerID's which exist. In the HTML code you hopefully will be able to see what I want to do - but I don't really understand how to do it properly. Any ideas are apprechiated.
In short: How do I fill out my with only 'CustomerId's that exist?
HTML Code:
@model PagedList.IPagedList<NowasteWebPortalMVC.Models.RegisterModel>
@using PagedList.Mvc;
<select name="customerId">
@foreach (var item in Model)
{
<option>@Html.DisplayFor(modelItem => item.CustomerId)</option>
}
</select>
RegisterController.cs
public class RegisterController : Controller
{
private UsersAndRolesDBContext db = new UsersAndRolesDBContext();
public ActionResult Register()
{
ViewBag.Title = "Portal";
var CustomerIds = (from Users in db.UsersAndRoles
select Users);
return View(CustomerIds.ToPagedList(1, 50));
}
}
RegisterModel.cs
namespace asdMVC.Models
{
[Table("UsersAndRoles")]
public class RegisterModel
{
[Key]
public int Id { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
public string Role { get; set; }
public int CustomerId { get; set; }
}
public class UsersAndRolesDBContext : DbContext
{
public UsersAndRolesDBContext() : base("MySqlConnection")
{
}
public DbSet<RegisterModel> UsersAndRoles { get; set; }
}
}
Thanks in advance :)
EDIT: Clarified text, made the page load again. But the select is empty now.
回答1:
If I understand correctly, you want to show a selectlist populated with customer details selected from the database.
The first thing to do is to add a SelectList
property to your ViewModel. I'm not sure what the PagedList is doing - do you use it solely to populate the selectlist? If so we don't need it. Your new ViewModel would look something like this:
public class RegisterUserViewModel
{
public SelectList UsersAsSelectList { get; set; }
}
In the controller you would populate the selectlist:
public ActionResult Register()
{
ViewBag.Title = "Portal";
var users = from u in db.UsersAndRoles
select new
{
u.Id, u.Name
};
var model = new RegisterUserViewModel
{
UsersAsSelectList = new SelectList(users, "Id", "Name");
}
return View(model);
}
And your view:
@model RegisterUserViewModel
@Html.DropDownList("Users", @Model.UsersAsSelectList )
There are other ways to do this and I've kept things separate for the sake of clarity. It can certainly be refactored later but it should get you started.
回答2:
HTML Code:
@model WebPortalMVC.Models.RegisterModel
@Html.LabelFor(m => m.SelectedCustomerId)
@Html.DropDownListFor(m => m.SelectedCustomerId, Model.CustomerID)
RegisterController.cs
public class RegisterController : Controller
{
private UsersAndRolesDBContext db = new UsersAndRolesDBContext();
public ActionResult Register()
{
ViewBag.Title = "Portal";
var model = new RegisterModel
{
CustomerID = GetCustomerIds()
};
return View(model);
}
public class DbCustomerIds
{
public List<DbCustomerId> GetCustomerIds()
{
List<DbCustomerId> Customers = new List<DbCustomerId>();
string queryString = "SELECT Id, Name FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString);
DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");
foreach (DataRow item in customers.Tables[0].Rows)
{
DbCustomerId cid = new DbCustomerId();
cid.Id = Convert.ToInt32(item["Id"]);
cid.Name = Convert.ToString(item["Name"]);
Customers.Add(cid);
}
return Customers;
}
}
private IEnumerable<SelectListItem> GetCustomerIds()
{
var DbCustomerIds = new DbCustomerIds();
var customers = DbCustomerIds
.GetCustomerIds()
.Select(x =>
new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
});
return new SelectList(customers, "Value", "Text");
}
RegisterModel.cs
namespace WebPortalMVC.Models
{
[Table("UsersAndRoles")]
public class RegisterModel
{
[Key]
public int Id { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
public string Role { get; set; }
public int CustomerId { get; set; }
[Display(Name = "Customer ID")]
public int SelectedCustomerId { get; set; }
public IEnumerable<SelectListItem> CustomerID { get; set; }
}
public class DbCustomerId
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UsersAndRolesDBContext : DbContext
{
public UsersAndRolesDBContext() : base("MySqlConnection")
{
}
public DbSet<RegisterModel> UsersAndRoles { get; set; }
}
}
回答3:
Stepwise:
1: Get all users from db
2: Create selectlistitems
3: Create dropdown with selectlist items
1:
public class RegisterUserViewModel
{
public List<SelectListItem> UsersSelectListItems { get; set; }
}
var users = from u in db.UsersAndRoles
select new
{
u.Id, u.Name
};
2:
var model = new RegisterUserViewModel
{
UsersSelectListItems = users.Select(u => new SelectListItem{Value = u.Id.ToString(), Text = u.Name}).ToList();
}
3:
@Html.DropDownList("Users", Model.UsersSelectListItems)
来源:https://stackoverflow.com/questions/32756535/how-do-i-create-a-html-select-with-only-items-in-database-in-asp-net-razor