MVC 5:NameForModel 渲染字段名称和 ID 的两倍
MVC 5 : NameForModel render twice the name and id of a field
我尝试修改编辑器模板以使用数据注释。我得到一些奇怪的东西。下面是 editorTemplate.
@model Translations<string>
@{
Translation<string> t = null;
if (Model != null && Model.Values != null)
{
t = Model.Values.FirstOrDefault(p => p.Language == KalowContext.Instance.CurrentLanguage);
}
t = t ?? new Translation<string>();
var validationAttributes = Html.GetUnobtrusiveValidationAttributes("");
}
@Html.TextBox(Html.NameForModel().ToString(), t.Value, new RouteValueDictionary(validationAttributes)
{
{ "class", "form-control" }
}
)
<input class="form-control" type="text" name="@(Html.NameFor(m => Model))" value="@t.Value">
这是输出,@Html.TextBox 会渲染两次 name 和 id 属性。在第二个输入中工作正常。
<input name="dc.Name.dc.Name" class="form-control" id="dc_Name_dc_Name" type="text" value="Afghanistan" data-val-required="The Name field is required." data-val="true">
<input name="dc.Name" class="form-control" type="text" value="Afghanistan">
有什么建议吗?谢谢!
这里是 class 翻译和翻译
public class Translations<T>
{
public List<Translation<T>> Values { get; set; }
public Translations()
{
}
}
public class Translation<T>
{
public ObjectId Language { get; set; }
public T Value { get; set; }
public Translation()
{
}
public Translation(ObjectId language, T value)
{
Language = language;
Value = value;
}
}
<input class="form-control" type="text" name="@(Html.NameFor(m => Model))"
@Html.Raw(string.Join(" ",
validationAttributes.
Select(x => x.Key.ToString() + "=\"" + x.Value + "\"")))
value="@t.Value">
中间解决方案...但是因为没有观众。我保持这样。 :=)
我尝试修改编辑器模板以使用数据注释。我得到一些奇怪的东西。下面是 editorTemplate.
@model Translations<string>
@{
Translation<string> t = null;
if (Model != null && Model.Values != null)
{
t = Model.Values.FirstOrDefault(p => p.Language == KalowContext.Instance.CurrentLanguage);
}
t = t ?? new Translation<string>();
var validationAttributes = Html.GetUnobtrusiveValidationAttributes("");
}
@Html.TextBox(Html.NameForModel().ToString(), t.Value, new RouteValueDictionary(validationAttributes)
{
{ "class", "form-control" }
}
)
<input class="form-control" type="text" name="@(Html.NameFor(m => Model))" value="@t.Value">
这是输出,@Html.TextBox 会渲染两次 name 和 id 属性。在第二个输入中工作正常。
<input name="dc.Name.dc.Name" class="form-control" id="dc_Name_dc_Name" type="text" value="Afghanistan" data-val-required="The Name field is required." data-val="true">
<input name="dc.Name" class="form-control" type="text" value="Afghanistan">
有什么建议吗?谢谢!
这里是 class 翻译和翻译
public class Translations<T>
{
public List<Translation<T>> Values { get; set; }
public Translations()
{
}
}
public class Translation<T>
{
public ObjectId Language { get; set; }
public T Value { get; set; }
public Translation()
{
}
public Translation(ObjectId language, T value)
{
Language = language;
Value = value;
}
}
<input class="form-control" type="text" name="@(Html.NameFor(m => Model))"
@Html.Raw(string.Join(" ",
validationAttributes.
Select(x => x.Key.ToString() + "=\"" + x.Value + "\"")))
value="@t.Value">
中间解决方案...但是因为没有观众。我保持这样。 :=)