我正在为某个viewModel属性开发客户端和服务器端验证.
在.cshtml
文件中我把它:
@Html.DropDownListFor(model => model.EntityType.ParentId, Model.ParentTypeList, "")
@Html.ValidationMessageFor(model => model.EntityType.ParentId)
Run Code Online (Sandbox Code Playgroud)
在Controller中进行业务验证
catch (BusinessException e)
{
ModelState.AddModelError("EntityType.ParentId", Messages.CircularReference);
}
Run Code Online (Sandbox Code Playgroud)
以上工作符合预期:如果捕获到异常,则消息将显示在下拉列表旁边.
但是,我发现这种方式并不是很优雅.在cshtml
,我使用一种方法来生成有关验证的所有必需信息.在控制器中,我必须知道确切的Key字符串并使用它.
这样做有没有更好的方法?
基本上标题是什么.这两者之间有什么区别(我目前正在使用第一个)
private EventHandler _progressEvent;
Run Code Online (Sandbox Code Playgroud)
和
private event EventHandler _progressEvent;
Run Code Online (Sandbox Code Playgroud)
我有一个方法
void Download(string url, EventHandler progressEvent) {
doDownload(url)
this._progressEvent = progressEvent;
}
Run Code Online (Sandbox Code Playgroud)
doDownload方法会调用
_progressEvent(this, new EventArgs());
Run Code Online (Sandbox Code Playgroud)
到目前为止,它工作正常.但我觉得我做的事情非常糟糕.
使用 FluentAssertions 6,您似乎可以更长地验证对象图中的 Enum 是否相当于字符串。来源:https :// Fluentassertions.com/upgradingtov6
enum MyEnum {
A,
B
}
class Source {
MyEnum Enum { get;set;}
}
class Expectation {
string Enum { get;set;}
}
var source = new Source() { Enum = MyEnum.A };
var expectation = new Expectation() {Enum = "A"};
//With V6 this assertion will fail but in V5 it will pass
expectation.Should().BeEquivalentTo(source, options => options.ComparingEnumsByName());
Run Code Online (Sandbox Code Playgroud)
如何使用 FluentAssertions 断言上述对象?我想要的行为是对枚举的 ToString 表示进行断言。
正如我附注的那样,当我与 交换时,我会得到不同的expectation
行为source
。它们不应该是等价的吗?