public class Itemlist
{
public string Text { get; set; }
public string Value { get; set; }
}
字符串 IndexViewModel.cs
public class IndexViewModel
{
public string Selected { get; set; }
public List<Itemlist> List1 { get; set; }
}
型 HomeController
public IActionResult Index(IndexViewModel? model)
{
if (model.List1 == null)
{
model = new IndexViewModel()
{
List1 = new List<Itemlist>()
{
new Itemlist { Text = "Physics", Value = "Physics" },
new Itemlist { Text = "Maths", Value = "Maths" },
}
};
}
return View(model);
}
public IActionResult AddSelect(IndexViewModel model,string newOption)
{
if (!model.List1.Any(x => x.Text == newOption)&&(newOption!=null))
{
model.List1.Add(new Itemlist { Text = newOption, Value = newOption });
}
return View("Index", model);
}
1条答案
按热度按时间kulphzqa1#
您可能需要逐个绑定selectlist值以发布旧列表。您可以尝试以下示例:
Itemlist.cs
字符串
IndexViewModel.cs
型
HomeController
型
Index.cshtml
型
测试
的数据