html listbox 属性,c# - Html.ListBoxFor not selecting Items - Stack Overflow

开发者在使用MVC 2.0时遇到问题:在控制器中,多选列表框的选中项在POST回后变为0。文章探讨了导致问题的原因,并尝试了不同解决办法,包括获取Repository数据和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Firstly I'm using MVC 2.0. I am having trouble getting a selection with a Multiselect listbox on the postback to my controller. Here is my Code:

Model

public class TestModel

{

public MultiSelectList AvailableTestTypes { get; set; }

public List SelectedTestTypes { get; set; }

}

public class TestType

{

public long Id { get; set; }

public string Name { get; set; }

public bool isActive { get; set; }

}

Controller

public ActionResult Index(TestModel model)

{

if (model == null) model = new TestModel();

List testList = new List()

{

new TestType() { Id = 1, Name = "TESTING", isActive = true },

new TestType() { Id = 2, Name = "TESTING2", isActive = true },

new TestType() { Id = 3, Name = "TESTING3", isActive = true }

};

model.AvailableTestTypes = testList; //TODO: Fetch From Repository

return View(model);

}

[HttpPost]

public ActionResult PostForm(TestModel model)

{

List act = model.SelectedTestTypes != null ? model.SelectedTestTypes.Select(x => x.Id).ToList() : new List();

View

Html.ListBoxFor(x => x.SelectedTestTypes,

new MultiSelectList(Model.AvailableTestTypes, "Id", "Name", Model.SelectedTestTypes),

new { @id = "testListboxId", @class = "blah", @title = "title" }) : null%>

After the Post to the Controller my Selected List count is 0 no matter how many I choose...

What am I getting wrong here?

I tried a few different solutions such as

Challenges with selecting values in ListBoxFor

but nothing worked :(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值