MVC 2数据注释问题(MVC 2 Data annotations problem)
现在生气了。 我有一个MVC解决方案,我已经从MVC 1升级到2.一切正常....除了验证!
这是一些代码:
在控制器中:
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Security.Principal; using System.Web; using System.Web.Mvc; using System.Web.Security; using System.Web.UI; using MF.Services.Authentication; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace MF.Controllers { //basic viewmodel public class LogOnViewData { [Required] public string UserName { get; set; } [Required] public string Password { get; set; } } [HandleError] public class AccountController : Controller { [HttpPost] public ActionResult LogOn(LogOnViewData lvd, string returnUrl) { if (ModelState.IsValid) { //do stuff - IsValid is always true } } } }
ModelState始终有效。 然而,该模型正在正确填充。 因此,如果我将用户名和密码都留空,并发布表单,则模型状态仍然有效。 哎呀!
额外信息:使用IoD的结构图。 以前,在升级到MVC 2之前使用的是MS数据注释库,所以在我的global.asax.cs中有这个:ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
现在已经删除了。
我确信我正在做一些非常基本和错误的事情。 如果有人能指出这将是奇妙的。 干杯
Going mad now. I have a MVC solution that i've upgraded from MVC 1 to 2. It all works fine.... except the Validation!
Here's some code:
In the controller:
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Security.Principal; using System.Web; using System.Web.Mvc; using System.Web.Security; using System.Web.UI; using MF.Services.Authentication; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace MF.Controllers { //basic viewmodel public class LogOnViewData { [Required] public string UserName { get; set; } [Required] public string Password { get; set; } } [HandleError] public class AccountController : Controller { [HttpPost] public ActionResult LogOn(LogOnViewData lvd, string returnUrl) { if (ModelState.IsValid) { //do stuff - IsValid is always true } } } }
The ModelState is always valid. The model is being populated correctly however. Therefore, if I leave both username and password blank, and post the form the model state is still valid. Argh!
Extra info: using structure map for IoD. Previously, before upgrading to MVC 2 was using the MS data annotation library so had this in my global.asax.cs: ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
Have removed that now.
I'm sure i'm doing something really basic and wrong. If someone could point it out that would be marvellous. Cheers
原文:https://stackoverflow.com/questions/2472025
最满意答案
我不知道如何将匹配的位置考虑到正则表达式的解决方案,但是如果将搜索字符串和术语转换为单词数组,则可以完成。
通过这种方法,我们迭代文本项并为搜索词中的每个词建立一个位置匹配数组,然后根据匹配数对匹配结果进行排序。
$search_words = explode(' ', strtolower($searchword)); foreach ($example as $item) { $item_words = explode(' ', strtolower($item)); // look for each word in the search term foreach ($search_words as $i => $word) { if (in_array($word, $item_words)) { // add the index of the word in the search term to the result // this way, words appearing earlier in the search term get higher priority $result[$item][] = $i; } } } // this will sort alphabetically if the uasort callback returns 0 (equal) ksort($result); // sort by number of matches, then position of matches uasort($result, function($a, $b) { return count($b) - count($a) ?: $a <=> $b; }); // convert keys to values $result = array_keys($result);
I don't know of a way to take the position of the matches into account with a regex solution, but if you convert the search string and the terms to arrays of words, it can be done.
With this approach, we iterate the text items and build an array of position matches for each word in the search term, then sort the result by number of matches, then position of matches.
$search_words = explode(' ', strtolower($searchword)); foreach ($example as $item) { $item_words = explode(' ', strtolower($item)); // look for each word in the search term foreach ($search_words as $i => $word) { if (in_array($word, $item_words)) { // add the index of the word in the search term to the result // this way, words appearing earlier in the search term get higher priority $result[$item][] = $i; } } } // this will sort alphabetically if the uasort callback returns 0 (equal) ksort($result); // sort by number of matches, then position of matches uasort($result, function($a, $b) { return count($b) - count($a) ?: $a <=> $b; }); // convert keys to values $result = array_keys($result);
相关问答
更多-
你可以尝试这样的事情: 理念: 循环遍历数据和条件,并创建具有计数和值的Map。 使用正则表达式匹配字符串。 这样您就可以进行不区分大小写的搜索。 根据计数对此地图进行排序。 返回值列表。 function getMatchCountMap(data, criterias) { return data.reduce((countMap, curItem) => { var count = criterias.filter((criteria) => new RegExp(criteria, ...
-
这是您需求的可读版本,但这不仅适用于生产环境,仅用于学术知识: Select if( INSTR(your_column, 'dog')>0 ,1,0) + if( INSTR(your_column, 'cat')>0 ,1,0) + if( INSTR(your_column, 'house')>0 ,1,0) as my_rate, * from ... order by my_rate desc ; 正确的方法是全文搜索 This is a readable ve ...
-
我会select_by重构一下select_by方法: ATTRIBUTES = %i[sport description matchup time] def self.select_by(sport) all.select do |object| ATTRIBUTES.any? { |attr| object.public_send(attr).include?(sport) } end end 添加更多的属性就像将属性名称添加到常量一样简单。 I would refactor th ...
-
搜索引擎有多个关键字(Search engine with multiple keywords)[2024-01-28]
您需要在foreach语句之前启动$ x变量,如果要将其用作整数,则不要将其设置为null。 $ construct变量有相同的错误,你必须有三次相同的响应,那是因为你必须在调用mysql select之前关闭foreach语句。 $x = 1; $construct = NULL; foreach($search_exploded as $search_each) { if($x==1) { $construct .="tag LIKE '%$search_each%'"; ... -
链接的答案是正确的做法。 为Drupal查询创建ORDER BY语句可以这样完成: $term = strip_tags(drupal_substr($_POST['keyword'], 0, 100)); $terms = explode(' ', $term); // split using ' ' $terms = array_map('trim', $terms); // remove unwanted spaces $termsfiltered = array_filter($terms); $ ...
-
你可以使用以下公式: =FIND("gross",A1,FIND("gross",A1)+2) 希望对你有效。 you can use the following formula: =FIND("gross",A1,FIND("gross",A1)+2) hope it works for you.
-
关于MySQL的可爱之处,取决于你的观点。 它会将数据视为您认为需要如何处理的数据。 1 + '1' ? 非常清楚你想把字符串视为一个数字,所以它很乐意为你执行添加。 也适用于布尔操作数(只需要观察运算符优先级)。 (a like '%foo%') + (a like '%bar%')识别数字上下文,将布尔结果true视为1 ,将布尔结果false视为0 - 基本上为您计算关键字匹配。 你怎么用? 拿你的where子句,替换所有or用+ ,并确保每个like语句都包含在括号中,然后order by 。 例如 ...
-
我不知道你为什么两次调用mysqli_query ,也许这是你的代码的上下文。 我建议你总是让SQL服务器来完成这些工作。 接口语言(尤其是php)可能不如SQL服务器那么高效。 我的建议是在WHERE子句中使用OR运算符,因此您只发送一个查询。 $selx = "SELECT * FROM sicks WHERE (tags LIKE '%{$_POST['a0']}%' OR tags LIKE '%{$_POST['a1']}%' OR tags LIKE '%{$_POST['a2']}%' O ...
-
我不知道如何将匹配的位置考虑到正则表达式的解决方案,但是如果将搜索字符串和术语转换为单词数组,则可以完成。 通过这种方法,我们迭代文本项并为搜索词中的每个词建立一个位置匹配数组,然后根据匹配数对匹配结果进行排序。 $search_words = explode(' ', strtolower($searchword)); foreach ($example as $item) { $item_words = explode(' ', strtolower($item)); // look ...
-
自动填充多个关键字(Autocomplete with multiple keywords)[2023-08-26]
如果我正确理解你的问题,你想显示匹配相同句子的多个单词的列表。 点击这里举例Testing ...