C# 列表框未过帐到MVC控制器
我有一个表单,它有一些文本区域和列表框。表单中的所有其他字段都已过帐到controller,但没有过帐到ListBox 以下是代码:C# 列表框未过帐到MVC控制器,c#,asp.net-mvc,razor,listbox,C#,Asp.net Mvc,Razor,Listbox,我有一个表单,它有一些文本区域和列表框。表单中的所有其他字段都已过帐到controller,但没有过帐到ListBox 以下是代码: <form method="post" action="/Admin/AddPost"> @Html.ValidationSummary(true) <div class="form-group"> <label>Title</label> <input typ
<form method="post" action="/Admin/AddPost">
@Html.ValidationSummary(true)
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="BlogTitle" placeholder="Title" value="@Model.BlogTitle" required>
</div>
<div class="form-group">
<label>Image</label>
<input type="text" class="form-control" name="BlogImage" value="@Model.BlogImage" placeholder="Image">
</div>
<div class="form-group">
<label>Description</label>
<textarea name="BlogDescription">@Model.BlogDescription</textarea>
</div>
<div class="form-group">
<label>Tags</label>
@Html.ListBox("Tags", Model.Tags, new { @class = "form-control", @name = "Tags", @multiple = "multiple" })
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
模型如下:
public class BlogModel
{
[HiddenInput]
public int BlogId { get; set; }
[Required(ErrorMessage = "Blog Title is required")]
public string BlogTitle { get; set; }
[Required(ErrorMessage = "Blog description is required")]
public string BlogDescription { get; set; }
public string BlogImage { get; set; }
public IEnumerable<SelectListItem> Tags { get; set; }
}
公共类博客模型
{
[HiddenInput]
public int BlogId{get;set;}
[必需(ErrorMessage=“需要博客标题”)]
公共字符串BlogTitle{get;set;}
[必需(ErrorMessage=“需要博客描述”)]
公共字符串BlogDescription{get;set;}
公共字符串BlogImage{get;set;}
公共IEnumerable标记{get;set;}
}
非常感谢您的建议。您的属性
标记
的类型为IEnumerable
,您无法将
绑定到复杂对象的集合。
发回一个简单值数组(所选选项的值)
例如,您的模型需要绑定到一个属性
public IEnumerable<int> SelectedTags { get; set; }
旁注:
new{@name=“…”}
-幸运的是它什么也不做,
决不要试图覆盖由生成的名称
属性
HtmlHelper
方法new{@multiple=“multiple”}
-theListBox()
和
ListBoxFor()
方法已经生成了TextBoxFor()和TextAreaFor()`方法生成
其他表单控件,以便获得正确的双向模型绑定
SelectListItem
构造函数(不仅默认为false,而且忽略该值
无论如何-它是SelectedTags
的值,它决定了什么是
选定)属性
标记
的类型为IEnumerable
,您无法将
绑定到复杂对象的集合。
发回一个简单值数组(所选选项的值)
例如,您的模型需要绑定到一个属性
public IEnumerable<int> SelectedTags { get; set; }
旁注:
new{@name=“…”}
-幸运的是它什么也不做,
决不要试图覆盖由生成的名称
属性
HtmlHelper
方法new{@multiple=“multiple”}
-theListBox()
和
ListBoxFor()
方法已经生成了TextBoxFor()和TextAreaFor()`方法生成
其他表单控件,以便获得正确的双向模型绑定
SelectListItem
构造函数(不仅默认为false,而且忽略该值
无论如何-它是SelectedTags
的值,它决定了什么是
选定)使用IEnumerable仅填充该框,并添加另一个属性以保存ListBox中选定项值的列表,如下所示:
public IEnumerable<string> SelectedTags { get; set; }
public IEnumerable SelectedTags{get;set;}
注意:SelectedTags的填充将取决于SelectListItem的创建方式。如果设置了SelectedTags.Value,SelectedTags将包含这些值,否则SelectedTags将用SelectedListItem.Text属性的值填充。仅使用IEnumerable填充框,并添加另一个属性以保存ListBox中的选定项值列表,如下所示:
public IEnumerable<string> SelectedTags { get; set; }
public IEnumerable SelectedTags{get;set;}
注意:SelectedTags的填充将取决于SelectListItem的创建方式。如果设置了SelectedTags.Value,则SelectedTags将包含这些值,否则SelectedTags将使用SelectedListItem.Text属性的值填充