我正在研究MVC2应用程序,并希望设置文本输入的maxlength属性.
我已经使用数据注释在Model对象上定义了stringlength属性,并且正确地验证了输入字符串的长度.
我不希望在模型已经拥有信息时手动设置max length属性,在我的视图中重复相同的设置.有没有办法做到这一点?
代码片段如下:
从模型:
[Required, StringLength(50)]
public string Address1 { get; set; }
Run Code Online (Sandbox Code Playgroud)
从视图:
<%= Html.LabelFor(model => model.Address1) %>
<%= Html.TextBoxFor(model => model.Address1, new { @class = "text long" })%>
<%= Html.ValidationMessageFor(model => model.Address1) %>
Run Code Online (Sandbox Code Playgroud)
我想避免做的是:
<%= Html.TextBoxFor(model => model.Address1, new { @class = "text long", maxlength="50" })%>
Run Code Online (Sandbox Code Playgroud)
我想得到这个输出:
<input type="text" name="Address1" maxlength="50" class="text long"/>
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
是否可以创建一个包含短划线字符的属性名称的对象?
我正在创建一个匿名对象,以便我可以使用Json.Net将其序列化为Json,并且我需要的其中一个属性包含一个' - '破折号字符.
我想要的一个例子是:
var document = {
condtions = new {
acl = "public-read",
bucket = "s3-bucketname",
starts-with = "test/path"
}
};
Run Code Online (Sandbox Code Playgroud)
我知道我可以在创建对象时用下划线替换短划线,然后在序列化字符串中替换它们,但是想知道在没有这种解决方法的情况下是否有语言方法可以执行此操作.
我需要一种从流中读取Excel文件的方法.它似乎不适用于ADO.NET的处理方式.
方案是用户通过FileUpload上传文件,我需要从文件中读取一些值并导入到数据库.
由于多种原因,我无法将文件保存到磁盘,也没有理由这样做.
那么,有谁知道从FileUpload流中读取Excel文件的方法?
再次xml ..
我想从一个集合中选择一组子节点(用于分页目的).
$nodes = $xml->query(//parent
/child[sex = 'male'
and position() >= 10
and position() < 21]);
Run Code Online (Sandbox Code Playgroud)
如果我没有记错的话,那就是选择第10到第20个孩子的男孩.
我需要的是选择集合中的前10-20(或30-40)个男性...
我确定我是一个菜鸟,并且已经做过这个,但周五......
偷看
我目前正在研究 ASP MVC 应用程序。并且想知道是否有一种方法可以在用户登录后更改 HttpContext.Current.User.Identity.Name。
我希望能够这样做以允许用户更改他/她的用户名,因此需要在他们完成后更改 HttpContext.Current.User.Identity.Name。
任何帮助都会很棒
以CSV格式从SSRS导出信息时,它总是在导出文件中的最后一行数据后面附加两个空行.
虽然我可以在导出后编辑文件并删除空行,但是可以防止SSRS首先产生两个空白行吗?
我试图使用Linq将数据库查询中的每一行投影到一个自定义类型作为其值的字典中.我不确定要执行此操作的LINQ语法?
这是我目前的尝试(不编译,但应该证明我正在尝试做什么).我在使用'select new ...'部分时遇到了麻烦.
public class MyClass
{
public Dictionary<int, CustomType> dict;
}
public class MyType{
string Name;
decimal Value;
}
var result = (from t in table
select new {
t.Id,
new MyType(t.Name,t.Value)
}).AsEnumerable().ToDictionary();
Run Code Online (Sandbox Code Playgroud)
回答:
谢谢杰森.我只是使用属性和自动初始化器而不是构造函数.工作代码类似于此(欢迎任何改进):
public class MyType {
public string Name {get;set;}
public decimal Value { get; set;}
}
Dictionary<int, CustomType> dict;
dict = (from t in table
select new {
id = av.Account.Id,
mt = new MyType { Name = t.Name, Value = t.Value }
}).ToDictionary(item …Run Code Online (Sandbox Code Playgroud) 我有一个asp.net页面,我让用户使用FileUpload控件上传图像文件.我允许他们上传png,gif和jpeg图片.我想限制他们可以上传的图像类型,特别是我想只允许他们上传RGB上有颜色模式的图像.
如何在.Net中以编程方式检查上载文件的颜色模式?
c# ×5
.net ×2
asp.net ×1
asp.net-mvc ×1
excel ×1
file-upload ×1
filestream ×1
image ×1
json.net ×1
linq ×1
position ×1
sql ×1
ssrs-2008 ×1
validation ×1
xml ×1
xpath ×1