我正在尝试编写自己的自定义验证属性,但我遇到了一些问题.
我试图写的属性是当用户登录时,密码将与确认密码进行比较.
namespace Data.Attributes
{
public class ComparePassword : ValidationAttribute
{
public string PasswordToCompareWith { get; set; }
public override bool IsValid(object value)
{
if (PasswordToCompareWith == (string)value)
{
return true;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是当我试图在模型文件中设置这样的属性时:
[Required]
[ComparePassword(PasswordToCompareWith=ConfirmPassword)]
public string Password { get; set; }
[Required]
public string ConfirmPassword { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误1非静态字段,方法或属性'Project.Data.Models.GebruikerRegistreerModel.ConfirmPassword.get'需要对象引用
似乎VS不接受confirmpassword该PasswordToCompareWith=ConfirmPassword部分.
我究竟做错了什么?
至少,我认为这与问题有关.我的情况是这样的:
我有许多具有公共字段的业务实体,每个实体都有该实体独有的自定义字段.所以在代码中,这被建模为一个EntityBase类,并且有许多类派生自此,例如,Derived.
为了创建一个可重用的UI,我有一个EntityBase.vbhtml看起来像这样的视图:
@ModelType EntityBase
@Using Html.BeginForm("Edit", Model.GetType.Name)
@* show the editor template for the derived type *@
@* !!the next line renders nothing!! *@
@Html.EditorFor(Function(x) Model, Model.GetType.Name)
[show a bunch of stuff common to all EntityBase objects]
End Using
Run Code Online (Sandbox Code Playgroud)
然后一个调用Derived.vbhtml派生类来执行此操作:
@ModelType Derived
[show an EditorFor for various Derived-specific fields]
Run Code Online (Sandbox Code Playgroud)
然后,当您导航到时\Derived\Edit\123,它将返回默认视图Derived\Edit.vbhtml,它只是这样做:
@Html.EditorForModel("EntityBase")
Run Code Online (Sandbox Code Playgroud)
通过这种方式,控制器只返回预期的默认Edit视图,这是对视图的一次性调用EntityBase,它执行其操作并调用它Derived来呈现它不知道的派生类内容.
我认为这不起眼,但它不起作用.如在视图代码中标记的那样,当我在基类视图中调用EditorForModel时,指定派生类的名称以用作模板时,它不会呈现任何内容.我测试过,如果我在顶级编辑模板中调用完全相同的代码行,它就可以正常工作.所以有一些关于MVC不喜欢的继承,但我看不出是什么.请帮忙!
更新:它可以正常工作,而不是EditorFor我使用Partial(并将相应的模板从EditorTemplates文件夹移动到Shared文件夹),但这不是一个很好的解决方案,因为我认为不遵循EditorFor模板的命名约定令人困惑.
也许是由于我对有保证和Hamcrest匹配器的经验不足,我还没有设法弄清楚如何正确地做这个断言
when().
get(url).
then().
header("my-header", lessThanOrEqualTo("60")); // should compare Integers not Strings
Run Code Online (Sandbox Code Playgroud)
一个明显的解决方案是从头中提取值,将其转换为Integer,然后手动执行断言.然而,这将破坏与放心工作的美丽.有没有办法在没有膨胀测试的情况下进行正确的比较?
我们想要从一个版本的jQuery升级到另一个版本.我们使用各种在线插件,并编写了许多我们自己的插件.现在的挑战是试图缓慢地缓慢移植所有脚本对象,而不需要完全重写.我有一个想法如何处理这个:
但我有疑问:
解释问题:
如果您的所有插件仅在单个页面的范围内存在,则可以轻松解决不同的版本:只需在页面级别而不是主页级别(duh!)执行文件包含.但是,生成在主页面或用户控件中的对象有点困难......因为它们需要特定版本才能正确运行.
这是我的想法:
插件的定义以匿名函数开头.
(function ($){<!- code goes here -->})(jQuery);
Run Code Online (Sandbox Code Playgroud)
我见过的所有依赖项都以此为出发点.
举例:jQuery依赖包括插件,如:ui.widget,ui.position,ui.core等.
那么如果我使用JavaScript对象引用每个版本的jQuery(及其依赖项)并将THAT OBJECT传递给各种内部和在线插件呢?
对象参考可能会像这样:
var jQueryVersion1_3_2 = function(){<!- paste the files contents here-->};
var jQueryVersion1_4_4 = function(){<!- paste the files contents here-->};
Run Code Online (Sandbox Code Playgroud)
插件:我的内部插件和在线插件仍可作为(普通)文件链接包含在内,但有以下更改
从这里开始:
// Plug-in X
(function ($){<!- its untouched code -->})(jQuery);
// Plug-in Y
(function ($){<!- its untouched code -->})(jQuery);
// Plug-in Z
(function ($){<!- its untouched code -->})(jQuery);
Run Code Online (Sandbox Code Playgroud)
......版本很糟糕!
到这个......
// Plug-in X
(function ($){<!- its untouched code …Run Code Online (Sandbox Code Playgroud) 我已经从谷歌添加了jQuery到我的网站.可怕的部分是谷歌服务器的完全缓慢.有时我的网站在尝试从googleapis加载jquery时停止:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
我可以做点什么吗?从其他服务器加载jquery会更好吗?(也许是我的?)当"等待来自ajax.google的回复"出现时,我可以等待30分钟没有任何反应,我必须重新加载页面才能显示它
我在表单中添加以下字段:
->add('interessi_profilo', 'entity', array(
'label' => 'Interessi (Tr)',
'class' => 'MyProfiloBundle:TipoInteresse',
'required' => true,
'multiple' => true,
'expanded' => true,
'property' => 'tipo',
'query_builder' => function(\My\ProfiloBundle\Entity\TipoInteresseRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
},
Run Code Online (Sandbox Code Playgroud)
我希望只有在选中至少一个复选框时才发送表单,如果可能的话,还有一个告诉用户的工具提示: at least one option must be selected
以下列模型为例:
namespace MyNamespace.Model
{
//you can only have two friends for the sake of simplicity in this example :P
public class Friends{
public Person NormalFriend { get; set; }
public Hipster HipsterFriend { get; set; }
}
public class Person{
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
public class Hipster : Person
{
public HatType HatPreference { get; set; }
public int CoolPoints { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
假设我们正在尝试添加我们的两个朋友,一个普通朋友和一个时髦朋友:
Create.cshtml: …
我需要能够用垂直线分隔两列:
<div class="container">
<div class="row">
<div class="col-md-6">
<p>some text here </p>
</div>
<div class="col-md-6">
<p>some more text here</p>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像的ViewModel:
{
empName: [
{ name: 'NAME1' },
{ name: 'NAME2' }
]
}
Run Code Online (Sandbox Code Playgroud)
我希望根据我empName在name使用switch语句循环遍历属性时显示不同的部门名称.这样输出是:
部门1
部门2
我尝试了以下方法:
<ul data-bind="foreach: empName">
<div data-bind="switch: name">
<div data-bind="case: 'Name1'">
Department 1
</div>
<div data-bind="case: 'Name2'">
Department 2
</div>
<div data-bind="case: $default">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但我得到以下输出:
部门1
部门2
部门1
部门2
我怎样才能做到这一点?
当使用Autofac注册对象并通过控制器解析中间件和MVC中间件时,实例是不同的.实例在每个生命周期范围内注册.
我在这里弹出了一个repro项目https://github.com/jakkaj/AutofacResolveIssue.
应用程序IUserService为中间件设置一个值,然后尝试在ValuesController后面读取该值.
这种技术在旧版本的ASP.NET中使用autofac.有什么想法发生了什么?
asp.net-mvc ×3
c# ×2
jquery ×2
asp.net ×1
asp.net-core ×1
attributes ×1
autofac ×1
checkbox ×1
css ×1
forms ×1
html5 ×1
inheritance ×1
java ×1
javascript ×1
knockout.js ×1
razor ×1
rest ×1
rest-assured ×1
symfony ×1
testing ×1
validation ×1
vb.net ×1