嗨,因为问题表明我有3个不同的表格,其中有数据;
第一个表(类型):
id type
1 Day
2 Month
3 Year
第二个表(服务器):
server
Europe
Asia
North America
South America
第三个表(状态)
status
Error
Verified
我必须创建一个包含3个需要的下拉菜单的日志,我认为如果我愿意使用来自数据库的数据进行dropdropmenu更新更容易更新。
这是我的AddLogModel
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ReviewLogs.Models
{
public class AddLogModel
{
[Display(Name = "Type: "),
Required(ErrorMessage = "Required")]
public List<string> type { get; set; }
[Display(Name = "Server: "),
Required(ErrorMessage = "Required")]
public List<string> server { get; set; }
[Display(Name = "Status: "),
Required(ErrorMessage = "Required")]
public List<string> status { get; set; }
}
}
我的主控制器:
public ActionResult AddLogReview(AddLogModel model)
{
if (Request.IsAuthenticated)
{
if (ModelState.IsValid)
{
using (var db = new reviewlogsEntities())
{
}
}
else
{
//Honestly I don't think this will ever be hit but lets just be safe
ModelState.AddModelError("", "Required Data Not Filled In");
}
return View(model);
}
else
{
return RedirectToAction("index", "home");
}
}
如何将我的数据库中的3个表格信息转换为3个单独的下拉列表。我无法更改表格布局,我得到了这个数据库。你能说一下这个视图的外观吗?对此,我真的非常感激!
答案 0 :(得分:0)
拥有类似
的模型public class Server
{
[Display(Name = "Server: ")]
[Required(ErrorMessage = "Required")]
public string server { get; set; }
}
在您的控制器中,您可以获取服务器列表并将其传递给视图
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
List<Models.Server> serverList = new List<Models.Server>()
{
new Server {server = "abc"},
new Server {server = "xyz"},
new Server {server = "def"}
};
return View(serverList);
}
在视图中获取下拉列表
@Html.DropDownListFor(model => model.Select(s => s.server),
Model.Select(s => new SelectListItem
{
Text = s.server, Value = s.server
}))
模型为@model IEnumerable<Models.Server>