MVC3 Html.DisplayFor - 是否有可能有这种控制生成的ID? [英] MVC3 Html.DisplayFor -- Is it possible to have this control generate an ID?

查看:93
本文介绍了MVC3 Html.DisplayFor - 是否有可能有这种控制生成的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够显示一些文本,但也有文本通过jQuery是可修改的。

 <%= Html.DisplayFor(型号=> model.DeviceComponentName)%GT;

如果我用EditorFor代替DisplayFor我会看到输入控件的ID。我不希望值是那样的编辑,虽然。所以,我已一个DisplayFor,但它不产生对元件的ID属性

如果我只是换了DisplayFor在一个div并做类似:

 < D​​IV ID =<%= ViewData.TemplateInfo.GetFullHtmlFieldName(DeviceComponentName)%>>
    <%= Html.DisplayFor(型号=> model.DeviceComponentName)%GT;
< / DIV>$('#DeviceComponentName)文本('为newValue')。

还是有实现这一目标的一个更清洁的方式?

更新:是否有不依赖于硬件codeD字符串的方法吗?东西联系的对象本身,所以如果我的财产名称更改我会得到一个编译错误?

另外,我用这code,但我没有看到一个ID值出现:

 < TD类=editableValue>
    &所述;% - 标签应编辑的,因此它需要一个ID,但只将由jQuery的被编辑,因此它不能成为一个EditorFor - %GT;
    <%= Html.DisplayFor(型号=> model.DeviceComponentName,新{ID =DeviceComponentName})%GT;
    <按钮式=按钮ID =ComponentTreeButton级=nestedDialogBu​​tton> ...< /按钮>
< / TD>


解决方案

要避免魔串的投入(在你的模特属性改变的情况下),你可以用一个扩展做到这一点。这也使得更干净code:

 公共静态MvcHtmlString DisplayWithIdFor<的TModel,TValue>(此的HtmlHelper<的TModel>的帮手,防爆pression< Func键<的TModel,TValue>>前pression,串wrapperTag =格)
{
    VAR ID = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(Ex$p$pssionHelper.GetEx$p$pssionText(ex$p$pssion));
    返回MvcHtmlString.Create(的String.Format(< {0} ID = \\{1} \\> {2}< / {0}>中,wrapperTag,ID,helper.DisplayFor(如pression)));
}

然后,只需使用这样的:

  @ Html.DisplayWithIdFor(X => x.Name)

会产生

 < D​​IV ID =名称>比尔和LT; / DIV>

或者,如果你希望它被包裹在一个跨度:

  @ Html.DisplayWithIdFor(X => x.Name,跨越)

这将使:

 <跨度ID =名称>比尔和LT; / SPAN>

非剃刀

对于非剃刀语法,只需使用这样的:

 <%= Html.DisplayWithIdFor(X => x.Name)%GT;

 <%= Html.DisplayWithIdFor(X => x.Name,跨越)%GT;

I want to be able to display some text, but also have the text be modifiable via jQuery.

<%= Html.DisplayFor(model => model.DeviceComponentName)%>

If I used EditorFor instead of DisplayFor I would see an ID for the input control. I do not want the value to be editable in that way, though. So, I have made it a DisplayFor, but it does not generate an ID property for the element.

Should I just wrap the DisplayFor in a div and do something like:

<div id="<%= ViewData.TemplateInfo.GetFullHtmlFieldName("DeviceComponentName") %>">
    <%= Html.DisplayFor(model => model.DeviceComponentName)%>
</div>

$('#DeviceComponentName').text('newValue');

Or is there a cleaner way of achieving this?

Update: Is there a way which doesn't depend on hard-coded strings? Something that ties to the object itself so if my property name changes I'd get a compile error?

Also, I am using this code, but I do not see an ID value appear:

<td class="editableValue">
    <%--Label should be editable, so it needs an ID, but only will be edited by jQuery so it can't be an EditorFor--%>
    <%= Html.DisplayFor(model => model.DeviceComponentName, new { id = "DeviceComponentName" })%>
    <button type="button" id="ComponentTreeButton" class="nestedDialogButton">...</button>
</td>

解决方案

To avoid 'magic string' inputs (in case your model properties change), you could do this with an extension. It also makes for much cleaner code:

public static MvcHtmlString DisplayWithIdFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string wrapperTag = "div")
{
    var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
    return MvcHtmlString.Create(string.Format("<{0} id=\"{1}\">{2}</{0}>", wrapperTag, id, helper.DisplayFor(expression)));
}

Then simply use it like this:

@Html.DisplayWithIdFor(x => x.Name)

Will produce

<div id="Name">Bill</div>

Or if you want it to be wrapped in a span:

@Html.DisplayWithIdFor(x => x.Name, "span")

Which will make:

<span id="Name">Bill</span>

Non-Razor

For non Razor syntax, you simply use it like this:

<%= Html.DisplayWithIdFor(x => x.Name) %>

and:

<%= Html.DisplayWithIdFor(x => x.Name, "span") %>

这篇关于MVC3 Html.DisplayFor - 是否有可能有这种控制生成的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆