首先,我是C#,Razor,ASP.NET.我想用‘列表’为性别做一个'DropDownListFor‘,但问题是我得到了标题中描述的错误信息。这是我的代码:
模型
namespace Hoofdstuk5_MvcStudentForm1.Models.ViewModels
{
public class StudentViewModel
{
public string Geslacht { get; set; }
}
}控制器
namespace Hoofdstuk5_MvcStudentForm1.Controllers
{
public class StudentController : Controller
{
// GET: Student/Index
public ActionResult Index()
{
return View();
}
// GET: Student/Index
[HttpPost]
public ActionResult Index(string groep)
{
ViewBag.Geslacht = new List<SelectListItem>
{
new SelectListItem { Text = "Female", Value = "1" },
new SelectListItem { Text = "Male", Value = "2" }
};
return View(groep);
}
}
}视图
@Html.DropDownListFor(model => model.Geslacht, ViewBag.Geslacht as IEnumerable<SelectListItem>, "--- Select ---", new { htmlAttributes = new { @class = "form-control" } })
@Html.DropDownListFor(model => model.Geslacht, new List<SelectListItem> {
new SelectListItem { Text = "Male", Value = "1" },
new SelectListItem { Text = "Female", Value = "2" }
}, "--- Select ---", new { htmlAttributes = new { @class = "form-control" } })
@Html.DropDownListFor(model => model.Geslacht, null, "--- Select ---", new { htmlAttributes = new { @class = "form-control" } })第二个'DropDownListFor‘工作,但第一个和第三个必须来自控制器,但它不工作,我不知道为什么?有人能帮帮我吗?
发布于 2017-01-21 21:17:23
该错误意味着DropDownListFor() (IEnumerable<SelectListItem>)的第二个参数是null,因此该方法期望第一个参数是IEnumerable<SelectListItem> (而它不是)
在第一个示例中,ViewBag.Geslacht的值是null,因为您没有在GET方法中填充它(尽管在POST方法中填充它)。将ViewBag.Geslacht = new List<SelectListItem>(...)添加到GET方法。
在第三个例子中,您具体地将第二个参数设置为null
发布于 2017-01-21 21:14:23
您将向视图发送一个字符串作为模型:
return View(groep);因此,您将得到该错误。你必须把模型传递给你的观点。创建如下模型:
// Give this a better name
public class SomeModel
{
public List<SelectListItem> Geslacht { get; set; }
}然后将控制器代码更改为:
// GET: Student/Index
[HttpPost]
public ActionResult Index(string groep)
{
var model = new SomeModel();
model.Geslacht = new List<SelectListItem>
{
new SelectListItem { Text = "Female", Value = "1" },
new SelectListItem { Text = "Male", Value = "2" }
};
return View(model);
}如果您需要更多的东西,那么只需添加更多的属性到您的模型。
发布于 2017-01-21 21:25:08
我建议你改变一下你的模式:
public class StudentViewModel
{
public string Geslacht { get; set; }
public IEnumerable<SelectListItem> Geslachts { get; set; }
public StudentViewModel()
{
Geslachts = GetGeslachts();
}
public StudentViewModel(string geslacht)
{
Geslacht = geslacht;
Geslachts = GetGeslachts();
}
private IEnumerable<SelectListItem> GetGeslachts()
{
return new List<SelectListItem>
{
new SelectListItem
{
Text = "Select ---",
Value = string.Empty
},
new SelectListItem
{
Text = "Male",
Value = "1"
},
new SelectListItem
{
Text = "Female",
Value = "2"
}
}
}
}然后您可以更改控制器,如下所示:
public class StudentController : Controller
{
// GET: Student/Index
public ActionResult Index()
{
return View(new StudentViewModel());
}
// GET: Student/Index
[HttpPost]
public ActionResult Index(string groep)
{
return View(new studentViewModel(groep));
}
}进行此更改意味着您还应该更改视图,以接受StudentViewModel。现在,在您的观点中,您可以有以下内容:
@model oofdstuk5_MvcStudentForm1.Models.ViewModels.StudentViewModel
@Html.DropDownListFor(model=>model.Geslacht, Model.Geslachts, new { htmlAttributes = new { @class = "form-control" } )https://stackoverflow.com/questions/41784546
复制相似问题