ModelState.isValid - 假 [英] ModelState.isValid - false

查看:117
本文介绍了ModelState.isValid - 假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想html页面从DROPDOWNLIST获取参数,并将其发送给我的控制器,创造新的Model对象,并将其插入到数据库中。

I would like to get parameter from dropdownList on html page and send it to my controller, create new Model object, and insert it into database.

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

it's my controller (two methods to create 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页面上的DropDownList:

it's my dropdownList on html page:

@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 的名单(参考许多到多),我想从DROPDOWNLIST组选定的元素这一模式。但在结果创建方法,我有一个空musicStyles ModelState.isValid ==假

it has a List of musicStyles(reference many-to-many), and I want to set selected element from dropdownList to this model. But in result of Create method I have a null musicStyles, and ModelState.isValid == false

推荐答案

A &LT;选择&GT; 只回发一个单一的价值 - 它不能绑定属性列表&LT; MusicStyle&GT; musicStyles 您需要可以绑定到

A <select> only posts back a single value - it cant bind to property List<MusicStyle> musicStyles You need a view model with properties you can bind to

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
}

这篇关于ModelState.isValid - 假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆