【问题标题】:ModelState.isValid - falseModelState.isValid - 假
【发布时间】:2015-06-21 13:15:53
【问题描述】:

我想从 html 页面的下拉列表中获取参数并将其发送到我的控制器,创建新的模型对象,并将其插入数据库。

这是我的控制器(创建 My_Model 的两种方法):

public ActionResult Create()
    {
        IEnumerable<MusicStyle> musicStyleList = db.MusicStyles.ToList();
        ViewData["musicStyles"] = new SelectList(musicStyleList);
        return View();
    }

    // POST: Bands/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "id,name,info,appearanceDate,musicStyles")] Band band)
    {
        IEnumerable<MusicStyle> musicStyleList;
        if (ModelState.IsValid)
        {
            musicStyleList = db.MusicStyles;
            ViewData["musicStyles"] = new SelectList(musicStyleList).ToList();
            db.Bands.Add(band);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        musicStyleList = db.MusicStyles;
        ViewData["musicStyles"] = new SelectList(musicStyleList).ToList();
        return View(band);
    }

这是我在 html 页面上的下拉列表:

@Html.DropDownList("musicStyles", "select style")

这是我的模特:

 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; }
}

它有一个musicStyles(参考多对多)列表,我想将下拉列表中的选定元素设置为此模型。但在Create 方法的结果中,我有一个null musicStylesModelState.isValid == false

【问题讨论】:

  • 尝试在你的视图中添加@Html.ValidationSummary(false),看看你提交表单后得到什么错误信息。
  • @ekad 我没有消息

标签: c# asp.net asp.net-mvc entity-framework


【解决方案1】:

&lt;select&gt; 仅回发单个值 - 它无法绑定到属性 List&lt;MusicStyle&gt; 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)

在 POST 方法中

[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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多