Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# ModelState.isValid-false_C#_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# ModelState.isValid-false

C# ModelState.isValid-false,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我想从html页面上的dropdownList中获取参数,并将其发送给我的控制器,创建新的模型对象,然后将其插入数据库 它是我的控制器(创建我的_模型的两种方法): 这是我的模型: public class Band { [Required] public int id { get; set; } [Required] [RegularExpression(@"^[a-zA-Z-\s\\\/*_]+$")] [StringLength(60, Mini

我想从html页面上的dropdownList中获取参数,并将其发送给我的控制器,创建新的模型对象,然后将其插入数据库

它是我的控制器(创建我的_模型的两种方法):

这是我的模型:

 public class Band
{
    [Required]
    public int id { get; set; }

    [Required]
    [RegularExpression(@"^[a-zA-Z-\s\\\/*_]+$")]
    [StringLength(60, MinimumLength = 1)]
    public string name { get; set; } 

    public string info { get; set; }

    [Required]
    [Display(Name = "Appearance date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime appearanceDate { get; set; }

    public List<MusicStyle> musicStyles { get; set; }
    public List<Song> songs { get; set; }
    public List<Album> albums { get; set; }
}
公共类频带
{
[必需]
公共int id{get;set;}
[必需]
[正则表达式(@“^[a-zA-Z-\s\\\/*.+$”)
[StringLength(60,最小长度=1)]
公共字符串名称{get;set;}
公共字符串信息{get;set;}
[必需]
[显示(Name=“出现日期”)]
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:dd/MM/yyyy}”)]
公共日期时间外观日期{get;set;}
公共列表musicStyles{get;set;}
公共列表歌曲{get;set;}
公共列表相册{get;set;}
}
它有一个
音乐样式列表
(参考多对多),我想将dropdownList中的选定元素设置为该模型。但是由于使用了
Create
方法,我得到了一个
null musicStyles
,并且
ModelState.isValid==false
a
只回发了一个值-它不能绑定到属性
列表musicStyles
,您需要一个带有可以绑定属性的视图模型

public class BandVM
{
  [Display(Name = "Music style")]
  [Required(ErrorMessage = "Please select a style")]
  public string SelectedMusicStyle { get; set; }
  public SelectList MusicStyleList { get; set; }
  ....
}
在控制器中

public ActionResult Create()
{
  BandVM model = new BandVM();
  model.MusicStyleList = new SelectList(db.MusicStyles);
  return View(model);
}
在我看来

@Html.LabelFor(m => m.SelectedMusicStyle)
@Html.DropDownListFor(m => m.SelectedMusicStyle, Model.MusicStyleList, "select style")
@Html.ValidationMessageFor(m => m.SelectedMusicStyle)
后置法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BandVM model) // NO Bind.Include!
{
  // model.SelectedMusicStyle will contain the selected value
  // Create a new instance of the data model and map the view model properties to it
  // Save and redirect
}

尝试在视图中添加
@Html.ValidationSummary(false)
,查看提交表单后收到的错误消息。@ekad我没有消息阅读此:
@Html.LabelFor(m => m.SelectedMusicStyle)
@Html.DropDownListFor(m => m.SelectedMusicStyle, Model.MusicStyleList, "select style")
@Html.ValidationMessageFor(m => m.SelectedMusicStyle)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BandVM model) // NO Bind.Include!
{
  // model.SelectedMusicStyle will contain the selected value
  // Create a new instance of the data model and map the view model properties to it
  // Save and redirect
}