문제

현재 ASP.NET MVC에서 웹 사이트의 관리자 백엔드를 구축하고 있습니다.

ASP.NET MVC 응용 프로그램에서 'EditorFor'도우미 방법을 사용하기 시작했습니다.

<div id="content-edit" class="data-form">
    <p>
        <%= Html.LabelFor(c => c.Title) %>
        <%= Html.TextBoxFor(c => c.Title)%>
    </p>
    <p>
        <%= Html.LabelFor(c => c.Biography) %>
        <%= Html.EditorFor(c => c. Biography)%>
    </p>
</div>

이 모델에서 '전기'필드는 [uihelper ( "html")]로 장식되었습니다.

'HTML'부분보기가 있습니다 (보기/공유/editortemplates 아래) :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>

<textarea class="html">
    <%= Model.ToString() %>
</textarea>

이제 'TextArea'의 'ID'속성을 다음과 같이 필드의 이름으로 설정하고 싶습니다.

<textarea id="Biography" class="html">
    ...
</textarea>

그러나 현재 설정을 통해 그렇게하는 방법을 볼 수 없습니다.

내가 생각할 수있는 것은 'value'속성과 '대조군'속성을 포함하는 'HTML'뷰 모델을 만드는 것입니다.

그러나 'system.xml.linq.xelement'가 아닌보기를 기반으로한다면 더 이상 'Editorfor'도우미 방법과 호환되지 않으며 모든 것을 수동으로 수행해야합니다.

아직 비슷한 문제가 있습니까?

도움이 되었습니까?

해결책

ViewData.templateInfo.htmlfieldprefix 속성에서 원하는 ID를 꺼낼 수 있어야합니다. 이와 같이:

<%@ Control Language="C#"
      Inherits="System.Web.Mvc.ViewUserControl<System.XML.Linq.XElement>" %>
<textarea id="<%= ViewData.TemplateInfo.HtmlFieldPrefix %>" class="html">
    <%= Model.ToString() %>
</textarea>

이것이 작동하는 이유를 보여주기 위해 다음은 TemplateHelpers.cs (MVC2 Preview 1 소스)의 위치가 있습니다.

ViewDataDictionary viewData = new ViewDataDictionary(html.ViewDataContainer.ViewData) {
    Model = modelValue,
    TemplateInfo = new TemplateInfo {
        FormattedModelValue = formattedModelValue,
        ModelType = modelType,
        HtmlFieldPrefix = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(expression),
        IsNullableValueType = (underlyingNullableType != null),
    }
};

위의 통화에서 "Expression"은 편집 된 속성의 이름으로 초기화 (통화 스택을 추가)합니다.

btw, @sperling 아래의 @sperling은 원래 놓친 세부 사항을 잡았습니다. HtmlHelper.IdAttributeDotReplacement, 당신은 당신이 HtmlPrefix 재산 HtmlHelper.IdAttributeDotReplacement.

다른 팁

이것을 사용하여 ID를 생성하고 있습니다 (모델 접두사 포함). 이름 속성을 원하는 경우 .replace () 부품을 건너 뜁니다.

<%=Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty).Replace(".", HtmlHelper.IdAttributeDotReplacement) %>

우리의 경우 우리는 사용해야했습니다 Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName ~와 함께 ExpressionHelper.GetExpressionText

면도기에서 다음과 같이 사용되었습니다.

           // hiddenFor was caching the value of this html input, and the value alone, nothing else on the page!
            Expression<Func<Web.ViewModels.ApiSettingsViewModel, int>> expression = (m => m.OrgApiLoginCredentials[i].OrgApiLoginId); 
        }
        <input type="hidden" value="@Model.OrgApiLoginCredentials[i].OrgApiLoginId" name="@Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression))" class="data-org-api-login-id"/>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top