Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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# ASP.NET MVC ListBoxFor Multiselect控件(值';1,2';无效。)_C#_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC ListBoxFor Multiselect控件(值';1,2';无效。)

C# ASP.NET MVC ListBoxFor Multiselect控件(值';1,2';无效。),c#,asp.net-mvc,C#,Asp.net Mvc,我有一个简单的MVC应用程序,它有一个多选列表,当前在提交时抛出以下错误:“1,2”值无效 型号 public class FormData { public int FormDataId { get; set; } [Required(ErrorMessage = "Please select at least one Type of Health Insurer")] [DisplayName("Type of Health Insurer")] publi

我有一个简单的MVC应用程序,它有一个多选列表,当前在提交时抛出以下错误:“1,2”值无效

型号

public class FormData
{
    public int FormDataId { get; set; }

    [Required(ErrorMessage = "Please select at least one Type of Health Insurer")]
    [DisplayName("Type of Health Insurer")]
    public IEnumerable<SelectListItem> InsurerTypes { get; set; }
}
公共类FormData
{
public int FormDataId{get;set;}
[必需(ErrorMessage=“请选择至少一种类型的健康保险公司”)]
[显示名称(“健康保险人类型”)]
公共IEnumerable保险类型{get;set;}
}
控制器

List<SelectListItem> typeItemList = new List<SelectListItem>();
        typeItemList.Add(new SelectListItem { Value = "1", Text = "Type A" });
        typeItemList.Add(new SelectListItem { Value = "2", Text = "Type B" });
        typeItemList.Add(new SelectListItem { Value = "3", Text = "Type C" });
        typeItemList.Add(new SelectListItem { Value = "4", Text = "Type D" });
        typeItemList.Add(new SelectListItem { Value = "5", Text = "Type E" });

        ViewBag.TypeItemList = typeItemList;

        if (ModelState.IsValid)
        {
            db.FormDatas.Add(formData);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(formData);
List typeItemList=new List();
添加(新的SelectListItem{Value=“1”,Text=“Type A”});
添加(新的SelectListItem{Value=“2”,Text=“Type B”});
添加(新的SelectListItem{Value=“3”,Text=“Type C”});
添加(新的SelectListItem{Value=“4”,Text=“Type D”});
添加(新的SelectListItem{Value=“5”,Text=“Type E”});
ViewBag.TypeItemList=TypeItemList;
if(ModelState.IsValid)
{
db.FormDatas.Add(formData);
db.SaveChanges();
返回操作(“索引”);
}
返回视图(formData);
查看

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.InsurerTypes, htmlAttributes: new { @class = "required control-label col-md-2" })
        <div class="col-md-10">
            @Html.ListBoxFor(m => m.InsurerTypes, new MultiSelectList(ViewBag.TypeItemList, "Value", "Text"), new { @class = "form-control", Multiple = "multiple" })
            @Html.ValidationMessageFor(model => model.InsurerTypes, "", new { @class = "text-danger" })
        </div>
    </div>
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.InsurerTypes,htmlAttributes:new{@class=“required control label col-md-2”})
@ListBoxFor(m=>m.InsurerTypes,新的MultiSelectList(ViewBag.TypeItemList,“Value”,“Text”),新的{@class=“formcontrol”,Multiple=“Multiple”})
@Html.ValidationMessageFor(model=>model.InsurerTypes,“,new{@class=“text danger”})
我看过以下文章寻求帮助,但运气不佳:

编辑-更新模型:

public class FormData
{
    public int FormDataId { get; set; }
    public IEnumerable<SelectListItem> InsurerTypes { get; set; }
    public IEnumerable<int> SelectedInsurers { get; set; }
}
公共类FormData
{
public int FormDataId{get;set;}
公共IEnumerable保险类型{get;set;}
公共IEnumerable SelectedInsurers{get;set;}
}

该模型现在似乎正在工作,因为我可以在db.FormDatas.Add(formData)上看到所选项目的列表。但是,它似乎没有保存到数据库中,因为该列只是空的。

您使用的列表框用于选择多个值-您的属性必须是
public IEnumerable FormDataId{get;set;}
和视图
@@Html.ListBoxFor(m=>m.FormDataId,Model.InsurerTypes,new{@class=“form control”})
另外,
[必需的]
[显示]
(而不是
[显示名称]
)属性需要在绑定到的属性上,而不是在
选择列表上。这是假设您希望所选选项值绑定到属性
FormDataId
-否则您需要另一个属性(例如)
public IEnumerable SelectedInsurers{get;set;}
绑定到。谢谢你,Stephen,这让我走得更远了。现在我要处理的问题是没有保存到数据库中的值。我已经更新了上面的模型。我应该使用什么样的SQL类型来存放列表值,nvarchar或int到目前为止都不起作用。您不能在数据库中使用is
IEnumerable
的列。您需要第二个表,用FK to第一个表来存储选定的值。您使用的列表框用于选择多个值-您的属性需要是
public IEnumerable FormDataId{get;set;}
,视图
@@Html.ListBoxFor(m=>m.FormDataId,Model.insureTypes,new{@class=“form control”})
另外,
[必需的]
[显示]
(而不是
[显示名称]
)属性需要在绑定到的属性上,而不是在
选择列表上。这是假设您希望所选选项值绑定到属性
FormDataId
-否则您需要另一个属性(例如)
public IEnumerable SelectedInsurers{get;set;}
绑定到。谢谢你,Stephen,这让我走得更远了。现在我要处理的问题是没有保存到数据库中的值。我已经更新了上面的模型。我应该使用什么样的SQL类型来存放列表值,nvarchar或int到目前为止都不起作用。您不能在数据库中使用is
IEnumerable
的列。您需要第二个表,用于使用FK to第一个表存储选定的值。