这是我在MVC 5中的代码:
@Html.EditorFor(model => model.myfloatvalue, new { @type = "number", @min = "0", @step = "0.01", @value = "0" })
Run Code Online (Sandbox Code Playgroud)
这是html代码:
<input class="text-box single-line" data-val="true" data-val-number="The field Fix Amount must be a number." data-val-required="The Fix Amount field is required." id="myfloatvalue" name="myfloatvalue" type="text" value="">
Run Code Online (Sandbox Code Playgroud)
不是
<input class="text-box single-line" data-val="true" data-val-number="The field Fix Amount must be a number." data-val-required="The Fix Amount field is required." id="myfloatvalue" name="myfloatvalue" type="number" min="0" step="0.01" value="0">
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
谢谢你的回复!
当我设置ViewBag.Title在Razor模板的顶部时,将Model.Title使用值ViewBag.Title而不是期望值填充编辑器模板文本框Model.Title.Html.EditorFor(x => Model.Title)显式调用不会产生此行为.
如何在ViewBag.Title不维护模型的自定义编辑器模板的情况下阻止编辑器模板显示值?
@{
ViewBag.Title = "Edit Post " + Model.Title;
}
@Html.EditorFor(x => Model.Title) @* renders "My Title" text box *@
@Html.EditorFor(x => Model) @* renders "Edit Post My Title" text box *@
Run Code Online (Sandbox Code Playgroud) 我有一个viewmodel,它有一个int StateID字段和一个string StateName字段,如下所示:
public class DepartmentViewModel : BaseViewModel, IModelWithId
{
// only show in edit mode
public int StateId { get; set; }
// only show in display mode
public string StateName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个使用的只读视图和使用DisplayForModel的更新视图EditorForModel.我希望DisplayForModel视图显示StateName属性,EditorForModel视图使用StateID属性(我实际上是基于此呈现下拉列表).
我无法弄清楚如何装饰我的viewmodel属性来创建此行为.
在调用时Html.EditorFor(m => m),m具有公共属性的公共类在哪里,为具有该属性的属性显示隐藏的输入和标签[HiddenInput].
public class User
{
[HiddenInput]
public Guid ID { get; set; } // should not be displayed in editor template
public string Name { get; set; } // should be editable
}
Run Code Online (Sandbox Code Playgroud)
EditorFor(...) 带标签的ID属性不受欢迎的结果<div class="editor-label">
<label for="ID">ID</label> <!-- Why is this here? -->
</div>
<div class="editor-field">
<input id="ID" name="ID" type="hidden" value="">
</div>
Run Code Online (Sandbox Code Playgroud) 至少,我认为这与问题有关.我的情况是这样的:
我有许多具有公共字段的业务实体,每个实体都有该实体独有的自定义字段.所以在代码中,这被建模为一个EntityBase类,并且有许多类派生自此,例如,Derived.
为了创建一个可重用的UI,我有一个EntityBase.vbhtml看起来像这样的视图:
@ModelType EntityBase
@Using Html.BeginForm("Edit", Model.GetType.Name)
@* show the editor template for the derived type *@
@* !!the next line renders nothing!! *@
@Html.EditorFor(Function(x) Model, Model.GetType.Name)
[show a bunch of stuff common to all EntityBase objects]
End Using
Run Code Online (Sandbox Code Playgroud)
然后一个调用Derived.vbhtml派生类来执行此操作:
@ModelType Derived
[show an EditorFor for various Derived-specific fields]
Run Code Online (Sandbox Code Playgroud)
然后,当您导航到时\Derived\Edit\123,它将返回默认视图Derived\Edit.vbhtml,它只是这样做:
@Html.EditorForModel("EntityBase")
Run Code Online (Sandbox Code Playgroud)
通过这种方式,控制器只返回预期的默认Edit视图,这是对视图的一次性调用EntityBase,它执行其操作并调用它Derived来呈现它不知道的派生类内容.
我认为这不起眼,但它不起作用.如在视图代码中标记的那样,当我在基类视图中调用EditorForModel时,指定派生类的名称以用作模板时,它不会呈现任何内容.我测试过,如果我在顶级编辑模板中调用完全相同的代码行,它就可以正常工作.所以有一些关于MVC不喜欢的继承,但我看不出是什么.请帮忙!
更新:它可以正常工作,而不是EditorFor我使用Partial(并将相应的模板从EditorTemplates文件夹移动到Shared文件夹),但这不是一个很好的解决方案,因为我认为不遵循EditorFor模板的命名约定令人困惑.
我有几个领域我大多数的我的意见显示在我的MVC5代码的第一APP: ,[created_date],[created_by],[modified_date]和[modified_by].对于用户,我想将这些也包括在我的Edit()视图中,但与我的其他字段不同,我不希望它们可编辑(通常创建/通过和修改/数据不应该是可编辑的).
这就是我Edit()目前在视图中声明的字段:
<div class="form-group">
@*@Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Created Date:</span>
<div class="col-md-10">
@Html.DisplayFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.created_by, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">By:</span>
<div class="col-md-10">
@Html.DisplayFor(model => …Run Code Online (Sandbox Code Playgroud) 我想做这样的事情,所以我可以创建一个模态对话框,我将用jQuery调用
<div class="modal" id="modalName" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Edit Contacts</h3>
</div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new Dictionary<string, object> { { "class", "form-horizontal" } }))
{
<div class="modal-body">
@Html.EditorFor(model => new ViewModel(), "ViewModelTemplateName")
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<button type="submit" class="btn btn-primary">
Submit</button>
</div>
}
</div>
Run Code Online (Sandbox Code Playgroud)
在这条线上
@Html.EditorFor(model => new ViewModel(), "ViewModelTemplateName")
Run Code Online (Sandbox Code Playgroud)
我收到了错误
模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式.
我不明白为什么它会关心实例的位置或内容(只要它的类型正确)
@ Html.Partial("〜/ Views/Shared/EditorTemplates/ViewModel.cshtml",新的ViewModel())可以解决这个问题,但是我必须声明模板的完整路径......这很糟糕.
这样有更好的方法吗?