我在创建操作的视图中遇到问题创建下拉列表。该视图仅显示学生编号,标题和说明的字段,而不显示计划和类别下拉列表列表。我不知道如何创建SelectList并将其传递给视图。我不知道是否做得对。
public class ServiceForm
{
[Required]
[Display(Name="Student Number")]
public int student_number { get; set; }
[Required]
[Display(Name="Program")]
public SelectList program { get; set; }
[Required]
[Display(Name = "Title")]
public string title { get; set; }
[Required]
[Display(Name = "Description")]
public string description { get; set; }
[Required]
[Display(Name = "Category")]
public SelectList category { get; set; }
}
public class Program
{
public int id { get; set; }
public string program_code { get; set; }
public string program_desc { get; set; }
}
public IEnumerable<ProgramList> GetAllPrograms()
{
var s = ds.programs.OrderBy(m => m.program_code);
return AutoMapper.Mapper.Map<IEnumerable<ProgramList>>(s);
}
private ServiceManager m = new ServiceManager();
private ProgramManager pm = new ProgramManager();
[HttpGet]
public ActionResult Create()
{
ServiceForm form = new ServiceForm();
form.program = new SelectList(pm.GetAllPrograms(), "id", "program_code");
List<SelectListItem> cat = new List<SelectListItem>();
cat.Add(new SelectListItem() { Text = "General", Value = "General", Selected = true });
cat.Add(new SelectListItem() { Text = "Grades", Value = "Grades", Selected = false });
cat.Add(new SelectListItem() { Text = "Time Table", Value = "TimeTable", Selected = false });
cat.Add(new SelectListItem() { Text = "Zenit Account", Value = "Zenit", Selected = false });
form.program = new SelectList(cat, cat.ElementAt(0));
return View(form);
}
[HttpPost]
public ActionResult Create(ServiceAdd newService)
{
if (!ModelState.IsValid)
{
var reform = AutoMapper.Mapper.Map<ServiceForm>(newService);
reform.program = new SelectList(pm.GetAllPrograms(), "id", "program_code");
List<SelectListItem> cat = new List<SelectListItem>();
cat.Add(new SelectListItem() { Text = "General", Value = "General", Selected = true });
cat.Add(new SelectListItem() { Text = "Grades", Value = "Grades", Selected = false });
cat.Add(new SelectListItem() { Text = "Time Table", Value = "TimeTable", Selected = false });
cat.Add(new SelectListItem() { Text = "Zenit Account", Value = "Zenit", Selected = false });
reform.program = new SelectList(cat, cat.ElementAt(0));
return View(reform);
}
return RedirectToAction("Create");
}
答案 0 :(得分:1)
型号
public class ServiceForm
{
[Required]
[Display(Name="Student Number")]
public int student_number { get; set; }
[Required]
[Display(Name="Program")]
public SelectList Programs { get; set; }
public string SelectedProgram { get; set; }
[Required]
[Display(Name = "Title")]
public string title { get; set; }
[Required]
[Display(Name = "Description")]
public string description { get; set; }
[Required]
[Display(Name = "Category")]
public SelectList Categories{ get; set; }
public string SelectedCategory { get; set; }
}
<强>控制器强>
[HttpGet]
public ActionResult Create()
{
ServiceForm form = new ServiceForm();
List<SelectListItem> cat = new List<SelectListItem>();
cat.Add(new SelectListItem { Text = "General", Value = "General", Selected = true });
cat.Add(new SelectListItem { Text = "Grades", Value = "Grades", Selected = false });
cat.Add(new SelectListItem { Text = "Time Table", Value = "TimeTable", Selected = false });
cat.Add(new SelectListItem { Text = "Zenit Account", Value = "Zenit", Selected = false });
form.Categories = cat;
//fill Programs property here...
return View(form);
}
查看强>
@model ServiceForm
@using(Html.BeginForm(...))
{
@Html.LabelFor(m=>m.Programs)
@Html.DropDownFor(x => x.SelectedProgram , Model.Programs)
@Html.LabelFor(m=>m.Categories)
@Html.DropDownFor(x => x.SelectedCategory, Model.Categories)
}
答案 1 :(得分:0)
您可以在Controller中执行以下操作:
public ActionResult Create()
{
var data = new ServiceForm();
data.Categories = new SelectList({"1", "category1"}, {"2", "category2"});
return View(data);
}
[HttpPost]
public ActionResult Create(ServiceForm form)
{
// validate the input and save
}
在你的Razor视图中,你有类似的东西:
@model ServiceForm
@using(Html.BeginForm())
{
@Html.DropDownFor(x => x.SelectedCategory, Model.Categories)
<!-- ... other fields and labels -->
}