- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 html 页面上的 dropdownList 获取参数并将其发送到我的 Controller ,创建新的模型对象,并将其插入数据库。
这是我的 Controller (创建 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
列表(引用多对多),我想将 dropdownList 中的选定元素设置为该模型。但是在 Create
方法的结果中,我有一个 null musicStyles
和 ModelState.isValid == false
最佳答案
A <select>
仅回发一个值 - 它无法绑定(bind)到属性 List<MusicStyle> musicStyles
您需要一个具有可以绑定(bind)到的属性的 View 模型
public class BandVM
{
[Display(Name = "Music style")]
[Required(ErrorMessage = "Please select a style")]
public string SelectedMusicStyle { get; set; }
public SelectList MusicStyleList { get; set; }
....
}
在 Controller 中
public ActionResult Create()
{
BandVM model = new BandVM();
model.MusicStyleList = new SelectList(db.MusicStyles);
return View(model);
}
在 View 中
@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
}
关于c# - ModelState.isValid - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964919/
这个问题是关于理解为什么一种技术比另一种更好。在 Angular 4/5 中,在模板中,您可以通过执行以下操作来实现相同的目的: 1) *ngIf-else 语法 content here
Page.IsValid 和 args.IsValid 是否相互依赖? 我的意思是,如果 args.IsValid="true",Page.IsValid 必须为“true”。 当然反过来说:如果 P
我正在编写一个 Controller 并为其进行单元测试,当时我遇到了两种方法(我认为同样有效)来做某事。我的所有模型都有一个 IsValid 属性,我可以检查该属性以询问模型是否有效。 在回发到 C
如我的问题所述。我真的不明白 date-fns 的 isValid 和 luxon 的 isValid 之间的区别。我有一个日期字符串变量和一个用 new Date() 调用日期字符串的变量。 我的日
这个问题已经有答案了: Getting AbstractMethodError while creating a connection to Oracle9 database with Tomcat
我正在使用自定义 register在 react-hook-form ,我无法获得 formState.isValid成为 true当我在输入中输入文本时(因此满足 required 条件)。 这是一
是否可以查看 knockout-validation plugin 中是否只有一个属性有效? 我在文档中看不到任何对它的引用,只能查看整个模型是否有效。 例如,我希望计算出的 observable 具
在Connection的方法中,我应该给它多少超时时间? :S 我不知道正常的超时是多少,应该花多长时间? :) 我不想要 isValid() 返回 false 如果它可以返回 true 如果它有更多
使用IsValid()验证电子邮件地址或URL格式是否可以防止XSS?指定其他格式时,它会否定XSS吗? 最佳答案 有效的URL仍可以包含攻击媒介: #isValid("url", "http://
我可以使用 isValid(0) 函数来检查连接是否仍然有效(alive)吗? 我遇到以下异常: com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcep
我正在使用 ASP.NET-MVC Core 2.1,我的代码中有这个 ViewModel 类 public class HomeViewModel { public
我有一个非常简单的 MVC 2 表单。它有两个下拉菜单,用户和角色。无论我选择什么,员工下拉列表都会通过验证,而角色下拉列表不会通过验证。尽管我计划实现一个,但没有默认的“空”选项,这就是为什么我需要
我正在尝试在文件生成程序中为某些状态建模, 在一个点上 : 我想检查数据的当前状态 如果数据有效,我想继续 否则,我想通知用户无法继续的原因 伪代码将类似于: if(isValid()){ w
我正在尝试在 this video 的帮助下根据属性的可用性验证名为 Book 的模型。我正在尝试按照有关如何验证 Backbone.js 模型的说明进行操作,但是当我使用 .isValid() 方法
我有一个 javascript 类 c我正在尝试调用它的方法 isValid()关于onClick输入提交按钮的事件。我有c目前在我的提交按钮下方的一组脚本标签中。当我点击提交按钮时,我得到 Unca
我正在为 Com 执行一项小型任务。科学课,这是一门初级课,但我之前没有编码经验,所以我连基础知识都吃力。我会问很多问题,我很抱歉。我们被要求创建一个方法来遍历字符串的所有字符,看看它们是 A G C
我想从 html 页面上的 dropdownList 获取参数并将其发送到我的 Controller ,创建新的模型对象,并将其插入数据库。 这是我的 Controller (创建 My_Model
尝试创建一个简单的 Jmeter 5.0 测试以使用 JDBC 连接配置连接到数据库。无法连接到我的本地 mySql 数据库。 [jdbc:mysql://localhost:3306]。我可以使用
我几乎总是想在回发时检查 ModelSate.IsValid 是否被调用。而且必须在每次回发开始时进行检查违反了 DRY 原则,有没有办法让它自动检查? 例子: [HttpPost("Register
我的模型类如下: public class PostInputViewModel { [Required] [MinLength(1)] [Ma
我是一名优秀的程序员,十分优秀!