我在MVC中有以下模型:
public class ParentModel
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public IEnumerable<ChildModel> Children { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我想显示父模型的所有子项时,我可以这样做:
@Html.DisplayFor(m => m.Children)
Run Code Online (Sandbox Code Playgroud)
然后我可以创建一个ChildModel.cshtml显示模板,DisplayFor将自动遍历列表.
如果我想为IEnumerable创建自定义模板怎么办?
@model IEnumerable<ChildModel>
<table>
<tr>
<th>Property 1</th>
<th>Property 2</th>
</tr>
...
</table>
Run Code Online (Sandbox Code Playgroud)
如何创建具有模型类型的显示模板,IEnumerable<ChildModel>然后在@Html.DisplayFor(m => m.Children)没有抱怨模型类型错误的情况下调用?
让我们说我们定义了Planets枚举:
public enum Planets
{
Sun = 0,
Mercury=5,
Venus,
Earth,
Jupiter,
Uranus,
Neptune
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Enum.IsDefined方法来查找字符串是否存在于枚举中.
Enum.IsDefined(typeof(Planets), "Mercury"); // result is true
Run Code Online (Sandbox Code Playgroud)
但是,然后我尝试了这个,它也返回true:
Enum.IsDefined(typeof(Planets), 5); // result is true again
Run Code Online (Sandbox Code Playgroud)
它是怎么来的?这种方法没有任何过载.它只有一个签名:
Enum.IsDefined(Type enumType, object value);
Run Code Online (Sandbox Code Playgroud)
为什么以及如何Enum.IsDefined搜索名称和价值?这对我来说真的很有趣,为什么他们这样选择?IMO制造超载会是更好的选择,不是吗?
假设我需要一个扩展方法,它只选择不同来源的必需属性.源可以是数据库或内存中的集合.所以我定义了这样的扩展方法:
public IQueryable<TResult> SelectDynamic<TResult>(
this IQueryable<T> source,
...)
Run Code Online (Sandbox Code Playgroud)
这适用于IQueryables.但是,我也必须为IEnumerables 调用此函数.
在这种情况下,我可以在以下帮助下调用它.AsQueryable():
myEnumerable.AsQueryable()
.SelectDynamic(...)
.ToList();
Run Code Online (Sandbox Code Playgroud)
两者都很好.我有这样的问题,如果两者都工作正常,在哪些情况下我必须为同一目的创建两个不同的扩展方法,一个与之合作IEnumerable,另一个与IQueryable?
我的方法必须在以下情况下向数据库发送查询Queryable.
例如,以下是命名空间.Select 内的扩展方法的来源System.Linq:
我再次重复我的主要问题:
我的方法必须发送查询到数据库Queryable,但不是在使用时IEnumerable.而就目前而言,我正在使用AsQueryable()枚举器.因为,我不想为此编写相同的代码Enumerable.可以有一些副作用吗?
我有一个DataGrid.但我想在CopyingRowClipboardContent事件中获得专注的细胞价值.但是e.ClipboardRowContent因为它返回了所有选定的单元格值SelectionUnit.我不能改变datagrid的选择单位.为了解决这个问题,我需要获得有针对性的细胞柱数.然后我将从中删除所有列值clipboarcContent.如何在CopyingRowClipboardContent事件中获得专注的细胞?
我知道是什么View Engine,我更喜欢使用Razor视图引擎,因为它在ASPX引擎上的语法简单.内置视图引擎为您执行几乎所有任务,然后在什么情况下我应该创建自己的视图引擎,
我搜索了它,但获得了如何创建它的答案,而不是何时以及为何创建它.
任何人都可以帮我描述实时情景吗?
您好我正在尝试为文本块提供默认值,如果返回的结果为null
这是我正在尝试的!
返回的只是我设置的字符串格式!
<TextBlock x:Name="NameTxtBlock" Grid.Column="0" Margin="0,0,40,0" FontFamily="Segoe UI" FontSize="14" Text="{Binding Name, StringFormat='Item Name: {0}'}" Padding="2">
<TextBlock.Style>
<Style TargetType="TextBlock" >
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=NameTxtBlock, Path=Text}" Value="{x:Null}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Text" Value="No Name Found" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=NameTxtBlock, Path=Text}" Value="{x:Static System:String.Empty}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Text" Value="No Name Found" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Run Code Online (Sandbox Code Playgroud) 假设我有这个模型:
public class Person
{
public bool IsApproved { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并且这个代码,我试着input用check类型渲染:
@Html.CheckBoxFor(x => x.IsApproved)
@Html.CheckBox("IsApproved")
Run Code Online (Sandbox Code Playgroud)
但是,结果是不同的:
// CheckBoxFor result
<input data-val="true" data-val-required="The IsApproved field is required." id="IsApproved" name="IsApproved" type="checkbox" value="true">
<input name="IsApproved" type="hidden" value="false">
// CheckBox result
<input id="IsApproved" name="IsApproved" type="checkbox" value="true">
<input name="IsApproved" type="hidden" value="false">
Run Code Online (Sandbox Code Playgroud)
如何以及为什么,第一个生成客户端验证的属性,而另一个没有?
更新:
交换@Html.CheckBoxFor和@Html.CheckBox的顺序后,标记元素的顺序没有改变.
我们必须使用来自 3rd 方供应商(perforce)的 api。直到今天,我们都能够在 .net 框架应用程序中引用和使用该 API。但是,现在我们正在重构我们的产品,当然我们决定使用新的 .net 环境,即 .net core 2.2。由于 Perforce 没有为 .net core 发布该库,我们决定将该库移植到 .net 标准。
所以,简而言之,我们下载了源代码,移植并添加到 .net core 项目中作为参考。
到现在为止还挺好。一件奇怪的事情是,在对该库进行了一些使用之后,我们ExecutionEngineException从该库中获取了信息,这会触发Environment.Failfast和终止应用程序。
一个更重要的信息是该库使用另一个本机库(p4bridge.dll)。
例外是这样的:
FailFast:
A callback was made on a garbage collected delegate of type 'p4netapi!Perforce.P4.P4CallBacks+LogMessageDelegate::Invoke'.
at Perforce.P4.P4Bridge.RunCommandW(IntPtr, System.String, UInt32, Boolean, IntPtr[], Int32)
at Perforce.P4.P4Bridge.RunCommandW(IntPtr, System.String, UInt32, Boolean, IntPtr[], Int32)
at Perforce.P4.P4Server.RunCommand(System.String, UInt32, Boolean, System.String[], Int32)
at Perforce.P4.P4Command.RunInt(Perforce.P4.StringList)
at Perforce.P4.P4CommandResult..ctor(Perforce.P4.P4Command, Perforce.P4.StringList)
at Perforce.P4.P4Command.Run(Perforce.P4.StringList)
at Perforce.P4.Client.runFileListCmd(System.String, Perforce.P4.Options, System.String, Perforce.P4.FileSpec[])
at Perforce.P4.Client.SyncFiles(System.Collections.Generic.IList`1<Perforce.P4.FileSpec>, Perforce.P4.Options)
Run Code Online (Sandbox Code Playgroud)
我已经知道与以下内容相关的消息 garbage …
我绑定DataGrid.ItemsSource属性List<PersonDetails>对象.我通过支持Silverlight的WCF服务获取数据.所以这个PersonDetails类是在Web Project中实现的.如果类位于Silverlight项目中,则每个DataGrid的标题文本都会根据需要进行更改.但是我不能在Web服务中使用这个类.唯一的解决方案是将相同的类添加到两个项目中.但是,还有其他方法吗?
这个类看起来像这样:
[DataContract]
public class PersonGeneralDetails
{
// Properties
[DataMember]
[DisplayAttribute(Name = "Sira")]
public int RowNumber { get; set; }
[DataMember]
[DisplayAttribute(Name = "Seriyasi")]
public string SerialNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
似乎在Web项目中不生成属性.我知道我可以使用DataGrid事件更改标题文本.但我想使用属性使其工作.
我想将 CompanyID 过滤器添加到我的所有实体框架请求中。因为每个用户必须只看到他们的记录。我不想在业务层中添加过滤器 (x=>x.CompanyID == cID) 的所有方法。如何自动添加过滤器要求。
我在 DAL 中的 GetList 方法
public List<TEntity> GetList(Expression<Func<TEntity, bool>> filter)
{
using (var context = new TContext())
{
return filter == null
? context.Set<TEntity>().ToList()
: context.Set<TEntity>().Where(filter).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
商业
public List<FinanceData> GetAll()
{
return _financeDal.GetList(filter:x=>x.CompanyID==_cID);
}
Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×3
asp.net-mvc ×3
asp.net ×2
wpf ×2
.net-core ×1
enumeration ×1
enums ×1
ienumerable ×1
iqueryable ×1
linq ×1
razor ×1
silverlight ×1
textblock ×1
wcf ×1
wpfdatagrid ×1
xaml ×1