我在视图中有以下代码:
<%= Html.ListBoxFor(c => c.Project.Categories,
new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>
<%= Html.ListBox("MultiSelectList",
new MultiSelectList(Model.Categories, "Id", "Name", new List<int> { 1, 2 }))%>
Run Code Online (Sandbox Code Playgroud)
唯一的区别是第一个帮助器是强类型的(ListBoxFor),并且它无法显示所选项(1,2),即使项目出现在列表中,等等.更简单的ListBox正在按预期工作.
我显然在这里遗漏了一些东西.我可以使用第二种方法,但这真的让我烦恼,我想弄清楚.
作为参考,我的模型是:
public class ProjectEditModel
{
public Project Project { get; set; }
public IEnumerable<Project> Projects { get; set; }
public IEnumerable<Client> Clients { get; set; }
public IEnumerable<Category> Categories { get; set; }
public IEnumerable<Tag> Tags { get; set; }
public ProjectSlide SelectedSlide { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我知道这个问题已经被问到了很多.
但我仍然无法弄清楚问题.
我正在开发一个博客来自学MVC框架.现在,当我发布下面的视图时,ListBoxFor帮助器不会将任何值绑定到我的模型.该列表成功包含所有类别,但是当POST控制器返回视图模型时,Categories对象为null.
这是视图模型:
public class PostViewModel
{
public Post Posts { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制者:
public ActionResult Create()
{
PostViewModel post = new PostViewModel();
post.Categories = db.ListCategories();
return View(post);
}
Run Code Online (Sandbox Code Playgroud)
风景:
<p>@Html.ListBoxFor(model => model.Categories, new MultiSelectList(Model.Categories, "CategoryID", "CategoryName"))</p>
Run Code Online (Sandbox Code Playgroud) 我的代码中有类似的东西,我收到错误:异常详细信息:System.ArgumentException:值不能为null或为空.参数名称:名称.我究竟做错了什么 ?感谢帮助
@model IEnumerable<NHibernateFluentProject.Patient>
@Html.ListBoxFor(model => model, new SelectList(Model,"ID", "FirstName"));
我有一个带有2个列表框(list1,list2)的网页,我可以将项目从一个列表框转移到另一个列表框并返回.当我单击提交按钮时,我想将list2中的所有项值返回到httppost方法.
我有3个问题:
模型:
public class TestModel
{
public List<SelectListItem> OptionList { get; set; }
public List<SelectListItem> SelectedOptionList { get; set; }
public string[] SelectedOptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
private TestModel testModel = new TestModel();
public ActionResult TestPage()
{
ViewBag.Message = "Test Page";
testModel.OptionList = new List<SelectListItem>();
for (int i = 1; i <= 100; i++)
{
SelectListItem item = new SelectListItem();
item.Text = "Option " …Run Code Online (Sandbox Code Playgroud)