cha*_*dmk 112 c# asp.net-mvc asp.net-mvc-2
Asp.Net MVC 2.0预览版提供了类似的帮助程序
Html.EditorFor(c => c.propertyname)
Run Code Online (Sandbox Code Playgroud)
如果属性名称是字符串,则上面的代码呈现texbox.
如果我想将MaxLength和Size属性传递给文本框或我自己的css类属性,该怎么办?
我是否需要在我的应用程序中为每种大小和长度组合创建一个模板?如果是这样,那么不会使默认模板可用.
WEF*_*EFX 91
在MVC3中,您可以设置宽度如下:
@Html.TextBoxFor(c => c.PropertyName, new { style = "width: 500px;" })
Run Code Online (Sandbox Code Playgroud)
tje*_*ans 61
我通过在我的/ Views/Shared/EditorTemplates文件夹中创建一个名为String.ascx的EditorTemplate来解决这个问题:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<% int size = 10;
int maxLength = 100;
if (ViewData["size"] != null)
{
size = (int)ViewData["size"];
}
if (ViewData["maxLength"] != null)
{
maxLength = (int)ViewData["maxLength"];
}
%>
<%= Html.TextBox("", Model, new { Size=size, MaxLength=maxLength }) %>
Run Code Online (Sandbox Code Playgroud)
在我看来,我用
<%= Html.EditorFor(model => model.SomeStringToBeEdited, new { size = 15, maxLength = 10 }) %>
Run Code Online (Sandbox Code Playgroud)
对我来说就像一个魅力!
way*_*mon 33
在为Html.EditorFor设置HTML属性的这个或任何其他线程中,没有任何答案对我有很大帮助.但是,我确实找到了一个很好的答案
我使用相同的方法,它工作得很漂亮,而无需编写大量额外的代码.请注意,设置了Html.EditorFor的html输出的id属性.视图代码
<style type="text/css">
#dob
{
width:6em;
}
</style>
@using (Html.BeginForm())
{
Enter date:
@Html.EditorFor(m => m.DateOfBirth, null, "dob", null)
}
Run Code Online (Sandbox Code Playgroud)
具有数据注释和日期格式为"dd MMM yyyy"的模型属性
[Required(ErrorMessage= "Date of birth is required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")]
public DateTime DateOfBirth { get; set; }
Run Code Online (Sandbox Code Playgroud)
工作就像一个魅力,而无需编写大量额外的代码.这个答案使用ASP.NET MVC 3 Razor C#.
小智 25
可能想看看Kiran Chand的Blog帖子,他在视图模型上使用自定义元数据,例如:
[HtmlProperties(Size = 5, MaxLength = 10)]
public string Title { get; set; }
Run Code Online (Sandbox Code Playgroud)
这与使用元数据的自定义模板相结合.在我看来,这是一个干净简单的方法,但我希望看到mvc内置的这个常见用例.
Ish*_*now 17
我很惊讶没有人提到在"additionalViewData"中传递它并在另一侧读取它.
查看(为了清晰起见,带有换行符):
<%= Html.EditorFor(c => c.propertyname, new
{
htmlAttributes = new
{
@class = "myClass"
}
}
)%>
Run Code Online (Sandbox Code Playgroud)
编辑模板:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%= Html.TextBox("", Model, ViewData["htmlAttributes"])) %>
Run Code Online (Sandbox Code Playgroud)
问题是,您的模板可以包含多个HTML元素,因此MVC将不知道应用哪个大小/类.你必须自己定义它.
使您的模板派生自您自己的类TextBoxViewModel:
public class TextBoxViewModel
{
public string Value { get; set; }
IDictionary<string, object> moreAttributes;
public TextBoxViewModel(string value, IDictionary<string, object> moreAttributes)
{
// set class properties here
}
public string GetAttributesString()
{
return string.Join(" ", moreAttributes.Select(x => x.Key + "='" + x.Value + "'").ToArray()); // don't forget to encode
}
Run Code Online (Sandbox Code Playgroud)
}
在模板中,您可以执行以下操作:
<input value="<%= Model.Value %>" <%= Model.GetAttributesString() %> />
Run Code Online (Sandbox Code Playgroud)
在您的视图中,您可以:
<%= Html.EditorFor(x => x.StringValue) %>
or
<%= Html.EditorFor(x => new TextBoxViewModel(x.StringValue, new IDictionary<string, object> { {'class', 'myclass'}, {'size', 15}}) %>
Run Code Online (Sandbox Code Playgroud)
第一个表单将呈现字符串的默认模板.第二个表单将呈现自定义模板.
替代语法使用流畅的界面:
public class TextBoxViewModel
{
public string Value { get; set; }
IDictionary<string, object> moreAttributes;
public TextBoxViewModel(string value, IDictionary<string, object> moreAttributes)
{
// set class properties here
moreAttributes = new Dictionary<string, object>();
}
public TextBoxViewModel Attr(string name, object value)
{
moreAttributes[name] = value;
return this;
}
Run Code Online (Sandbox Code Playgroud)
}
// and in the view
<%= Html.EditorFor(x => new TextBoxViewModel(x.StringValue).Attr("class", "myclass").Attr("size", 15) %>
Run Code Online (Sandbox Code Playgroud)
请注意,您可以在控制器中执行此操作,也可以在ViewModel中更好地执行此操作:
public ActionResult Action()
{
// now you can Html.EditorFor(x => x.StringValue) and it will pick attributes
return View(new { StringValue = new TextBoxViewModel(x.StringValue).Attr("class", "myclass").Attr("size", 15) });
}
Run Code Online (Sandbox Code Playgroud)
另请注意,您可以创建基本TemplateViewModel类 - 所有视图模板的共同基础 - 它将包含对属性/等的基本支持.
但总的来说,我认为MVC v2需要更好的解决方案.它仍然是Beta - 去问它;-)
我认为使用CSS是可行的方法.我希望我能用.NET编码做更多的事情,比如在XAML中,但在浏览器中,CSS是王道.
的site.css
#account-note-input {
width:1000px;
height:100px;
}
Run Code Online (Sandbox Code Playgroud)
.cshtml
<div class="editor-label">
@Html.LabelFor(model => model.Note)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Note, null, "account-note-input", null)
@Html.ValidationMessageFor(model => model.Note)
</div>
Run Code Online (Sandbox Code Playgroud)
乔
与MVC 5一样,如果您希望添加任何属性,您可以这样做
@Html.EditorFor(m => m.Name, new { htmlAttributes = new { @required = "true", @anotherAttribute = "whatever" } })
Run Code Online (Sandbox Code Playgroud)
从此博客中找到的信息
我写了一篇博客文章来回答我自己的问题
添加对模板的 html 属性支持 - ASP.Net MVC 2.0 Beta
| 归档时间: |
|
| 查看次数: |
208803 次 |
| 最近记录: |