Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表框未过帐到MVC控制器_C#_Asp.net Mvc_Razor_Listbox - Fatal编程技术网

C# 列表框未过帐到MVC控制器

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

我有一个表单,它有一些文本区域和列表框。表单中的所有其他字段都已过帐到controller,但没有过帐到ListBox

以下是代码:

 <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”}
    -the
    ListBox()
    ListBoxFor()
    方法已经生成了
  • 使用
    TextBoxFor()和
    TextAreaFor()`方法生成
    其他表单控件,以便获得正确的双向模型绑定
  • 删除
    SelectListItem
    构造函数(不仅默认为false,而且忽略该值 无论如何-它是
    SelectedTags
    的值,它决定了什么是 选定)

  • 属性
    标记
    的类型为
    IEnumerable
    ,您无法将
    绑定到复杂对象的集合。
    发回一个简单值数组(所选选项的值)

    例如,您的模型需要绑定到一个属性

    public IEnumerable<int> SelectedTags { get; set; }
    
    旁注:

  • 不要使用
    new{@name=“…”}
    -幸运的是它什么也不做, 决不要试图覆盖由生成的
    名称
    属性
    HtmlHelper
    方法
  • 不要使用
    new{@multiple=“multiple”}
    -the
    ListBox()
    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属性的值填充