在我的MVC项目中,我有不同的自定义验证属性.其中之一是检查属性的值与另一个属性的值.
正如许多文章中所述,我添加了类似的内容
result.ValidationParameters.Add("otherproperty", _otherPropertyHtml);
result.ValidationParameters.Add("comparetype", _compareType);
result.ValidationParameters.Add("equalitytype", _equalityType);
Run Code Online (Sandbox Code Playgroud)
返回的ModelClientValidationRule对象.
我现在的问题是,如果我要检查的属性被封装在另一个对象中,验证将无效.
如果我创造类似的东西
@Html.TextBoxFor(m => m.ValueOne)
@Html.TextBoxFor(m => m.ValueTwo)
Run Code Online (Sandbox Code Playgroud)
验证将在呈现时正常工作
data-val-otherproperty="ValueTwo"
Run Code Online (Sandbox Code Playgroud)
我的问题是以下几点
@Html.TextBoxFor(m => m.IntermediateObject.ValueOne)
@Html.TextBoxFor(m => m.IntermediateObject.ValueTwo)
Run Code Online (Sandbox Code Playgroud)
这将呈现两个带有名称IntermediateObject_ValueOne和的文本框IntermediateObject.ValueTwo.但仍然是第一个文本框的data-val-otherproperty ="ValueOne".
如何才能实现,这data-val-otherproperty一直是其他财产的正确名称?
我的想法类似于HtmlHelper <>.NameFor(m => ...)或使用反射的东西?
更新1 - 根据评论的要求添加代码
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false)]
public class CustomCompareToOther : ValidationAttribute, IClientValidatable
{
// private backing-field
private readonly string _otherPropertyName;
// constructor
public OemCompareToOther(string otherPropertyName)
{
_otherPropertyName = otherPropertyName;
}
// implementation of IClientValidatable
public IEnumerable<ModelClientValidationRule> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Linq/LinqKit 构建嵌套查询。从理论上讲,这似乎很容易。但我坚持实践部分。
在我的数据库中,我有一个表,该表具有对其父级的自引用。在我的 linq-query 中,我现在想要选择给定元素的所有父元素(以及该元素的父元素,依此类推)。
在我的代码中,我的部分类中有以下表达式MyTable:
public static Expression<Func<MyTable, IEnumerable<MyTable>>> Parents => (entity) => entity.ParentId != null ? new[]{entity.ParentEntity}.Union(Parents.Invoke(entity.ParentEntity) : new MyEntity[]{};
Run Code Online (Sandbox Code Playgroud)
它应该选择给定实体的父级以及ParentId设置时的那些父级。
查询本身(简化):
dbContext
.MyTable
.AsExpandable()
.Where(x => x.Id == myId)
.Select(x => new
{
Parents = MyTable.Parents.Invoke(x, dbContext)
});
Run Code Online (Sandbox Code Playgroud)
运行此代码最终会出现 in StackOverflowException,因为未达到停止条件,因此Parents-call 会无限嵌套,直到堆栈已满。
有什么想法可以做到这一点还是不可能?或者是否有其他方法可以在一个查询中使用Linq/获取嵌套数据LinqKit?
我已经尝试将上下文传递给表达式以创建子查询(也不起作用):
public static Expression<Func<MyTable, MyContext, IEnumerable<MyTable>>> Parents => (entity, dbContext) => entity.ParentId != null ? new[]{entity.ParentEntity}.Union(Parents.Invoke(dbContext.MyTable.FirstOrDefault(x => x.Id == entity.ParentId), dbContext) : …Run Code Online (Sandbox Code Playgroud) 在我的打字稿中,我试图通过基类中的方法创建/克隆子对象.这是我的(简化)设置.
abstract class BaseClass<TCompositionProps> {
protected props: TCompositionProps;
protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); } // can be overwriten by childs
constructor(props: TCompositionProps){
this.props = props;
}
clone(){
const props = this.cloneProps();
return this.constructor(props);
}
}
interface IProps {
someValues: string[];
}
class Child extends BaseClass<IProps>{
constructor(props: IProps){
super(props);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我要创建一个新对象
const o1 = new Child({someValues: ["This","is","a","test"]};
// get the clone
const clone = o1.clone();
Run Code Online (Sandbox Code Playgroud)
命中构造函数(但它只是对函数的调用),这意味着没有创建新对象.使用时,return Child.prototype.constructor(props)我得到了我的新对象.
那么如何调用Child其基类中的构造函数呢?
也尝试了这个
我目前正在使用 blazor 只是为了测试它的功能。
我有一个主要项目,它将作为向用户显示的最终网站。对于组件,我创建了一个类库,用于保存基于引导程序的组件,例如Table它将呈现应用了引导程序类的表。由于我最终将拥有多个网站,因此另一个类库项目中的网站之间也会存在共享组件。这个项目还将有一个名为的组件Table,它将呈现来自其他共享项目的引导表,并提供排序、分页、过滤等附加处理。
我遇到的问题是,存在我无法解决的命名冲突。
假设项目是Company.Website1根据最终网站、Company.Shared.Components扩展表命名的,并且Company.Shared.Components.Bootstrap它将保存由其他共享项目使用的引导组件。
当我尝试Table在中创建我的组件时,Company.Shared.Components出现以下错误
多个组件使用标签“Table”
我尝试了这里写的内容,但后来出现了错误
找到具有意外名称“Table.Table”的标记元素。如果这是一个组件,请为其命名空间添加 @using 指令
我还尝试为 using 指令添加别名,但没有任何机会。
剃刀文件本身很简单
@using Company.Shared.Components.Bootstrap.Table
<Table></Table>
Run Code Online (Sandbox Code Playgroud)
我想如果我使用第三方库,其中一些组件的名称与我的项目中已存在的组件的名称相同,我想我会得到相同的错误。所以必须有一个我目前看不到的解决方法。
正如标题所说,我想将一个属性从我的ViewModel绑定到相应视图中的嵌套UserControl.我不能按照我需要的方式工作.
嵌套的UserControl只不过是DatePicker一个DropDown小时和一个小时.如何告诉DatePicker我们选择ViewModel传播的日期作为其选定日期?
我几乎尝试了一切,现在我离窗户不远了.如你所见,任何帮助表示赞赏;)
现在到目前为止的代码:DateTimePicker.xaml.cs(CodeBehind)
public partial class DateTimePicker
{
public static DependencyProperty SelectedDateValueProperty = DependencyProperty.Register("SelectedDateValue", typeof (DateTime), typeof (DateTimePicker), new FrameworkPropertyMetadata(default(DateTime), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChangedCallback));
private static void OnPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
Debug.WriteLine("Wohoo. I'm here and still debugging...");
}
public DateTimePicker()
{
InitializeComponent();
DataContext = this;
var times = GetTimes();
Times.ItemsSource = times;
Times.SelectedItem = times.First();
}
public DateTime SelectedDateValue
{
get { return (DateTime) GetValue(SelectedDateValueProperty); }
set { SetValue(SelectedDateValueProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
嵌套的UserControl(DateTimePicker.xaml): …
考虑以下通用抽象类的实现:
public abstract class BaseRequest<TGeneric> : BaseResponse where TRequest : IRequestFromResponse
{
public TGeneric Request { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有没有机会获得属性的名称Request而没有从中继承的实例?
我需要Request字符串"Request"来避免使用硬编码字符串.任何想法如何通过反思来做到这一点?
我将为我的应用程序构建一个使用许多向导的帮助程序.对于我的观点,有一个简单的电话:
@using (var wiz = MyHelper.EditWizard(Translate(Keys.User.ChangePasswordTitle)))
{
// RenderPartial(...)
}
Run Code Online (Sandbox Code Playgroud)
而是MyHelper一个自己的实现,HtmlHelper其原始的helper-object被封装为一个属性.
由于向导可以包含多个步骤,因此可以将内容拆分为多个部分视图.变量wiz有一些我需要在部分中访问的公共方法.
问题是,我怎样才能通过wiz-object?
在里面,EditWizard()我正在尝试将向导添加到ViewData.
myHelper.HtmlInternal.ViewData["currentWizard"] = theWizard;
Run Code Online (Sandbox Code Playgroud)
但是,在我的部分中,ViewData-dictionary总是为空的.目前我尝试获取数据
var wiz = (Wizard)ViewData["currentWizard"];
Run Code Online (Sandbox Code Playgroud)
但wiz总是如此null.
我如何创建以下内容XElement?
<data name="MyKey" xml:space="preserve">
<value>Date of birth</value>
<comment>Some comment</comment>
</data>
Run Code Online (Sandbox Code Playgroud)
它抛出
“‘:’字符(十六进制值 0x3A)不能包含在名称中。”
var data = new XElement("data");
data.Add(new XAttribute("name", translation.Key));
data.Add(new XAttribute("xml:space", "preserve")); // <-- here is the error
data.Add(new XElement("value") { Value = "Date of birth" });
data.Add(new XElement("comment") { Value = "Some comment" });
Run Code Online (Sandbox Code Playgroud)
由于这是文件的一部分ResX,因此会有许多这样的<data></data>元素。
在我的数据库中,我有一个定义了一对多关系的条目。
我只想要一个简单的Lookup设定关系。
简单的例子:
汽车的值(如制造商和类型)位于同一个表中,属性(如颜色、马力、发动机)在另一个表中定义。(这只是一个例子,从数据库的角度来看没有意义)。
我想要的只是选择所有汽车,例如Ford通过它们的属性进行查找,但我不明白这一点。
目前我有
dbContext.Cars
.Where(c => c.Manufacturer == "Ford")
.Select(c => new {
c.Type,
Attributes = c.Attributes.Select(a => new{
a.Value
})
})
.ToLookup(arg => arg.Attributes.Select(a => a.Value), arg => arg.Type);
Run Code Online (Sandbox Code Playgroud)
但这给出了一个奇怪的组合查找
我需要像这样的查找
[500hp] : {Type1,Type2}
[300hp] : {Type3,Type6}
[green] : {Type3,Type7}
[blue] : {Type2,Type1}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从名为 的 IQueryable 上的一串属性名称(由用户给出)构建动态表达式source。这是我到目前为止所拥有的:
var parameter = Expression.Parameter(source.ElementType, "x");
var member = propertyChain.Split('.').Aggregate((Expression)parameter, Expression.PropertyOrField);
var selector = Expression.Lambda(member, parameter);
Run Code Online (Sandbox Code Playgroud)
这会给我一些类似x => x.MainProperty.SubProperty输入时的信息MainProperty.SubProperty。
我现在需要添加ToString()到表达式中selector,以便它将生成表达式x => x.MainProperty.SubProperty.ToString(),然后可以将其传递到其他方法中。
如何才能做到这一点?
我正在尝试建立一种动态GroupBy,其中密钥的类型并不重要。但分组依据的属性可以是类型Guid或int其他类型。这就是为什么我需要打电话ToString()。
public static IEnumerable<IGrouping<string, T>>(IQueryable<T> source, string propertyChain)
{
var parameter = Expression.Parameter(source.ElementType, "x");
var member = propertyChain.Split('.').Aggregate((Expression)parameter, Expression.PropertyOrField);
var selector = Expression.Lambda(member, parameter);
// currently here I have x => x.MainProperty.SubProperty
// …Run Code Online (Sandbox Code Playgroud) c# ×8
reflection ×3
asp.net-mvc ×2
generics ×2
linq ×2
binding ×1
blazor ×1
constructor ×1
expression ×1
inheritance ×1
javascript ×1
linq-to-xml ×1
linqkit ×1
lookup ×1
mvvm ×1
nested ×1
razor ×1
typescript ×1
validation ×1
viewdata ×1
wpf ×1
xml ×1