2017-09-28 2 views
0

Je dois factoriser un certain code HTML de MVC vue Razor et l'utiliser comme vue partielleComment lier un réseau de classe sur la même longueur d'onde, mais donner à chaque jeu de commandes pour un objet même indice ID

@helper NotificationTemplateHTML(int i) 
{ 
    <div class="tab-pane @Model.NotificationTemplates[i].ActiveCSSClass" id="@string.Format(@Model.NotificationTemplates[i].NotificationTypeString)"> 
     <p class="lead"> 
     </p> 
     @Html.HiddenFor(model => model.NotificationTemplates[i].NotificationType) 
     <p> 

      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].ParametersString, "Parameters") 
       @Html.HTTextBoxFor(model => model.NotificationTemplates[i].ParametersString, false, true) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].IsStopped, "Stop sending") 
       @Html.HTCheckedFor(model => model.NotificationTemplates[i].IsStopped, "Stop sending", true) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].IsLogEnabled, "Log Notification") 
       @Html.HTCheckedFor(model => model.NotificationTemplates[i].IsLogEnabled, "Log Notification", true) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].SendingNotificationType, "Sending Options") 
       @Html.HTMultiListFor(model => model.NotificationTemplates[i].SendingNotificationType, 
        FillListHelper.GetList<SendingNotificationType_s_WS, SendingNotificationType_s>(), true) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].MobileCC, "List mobile CC ',' seperated") 
       @Html.HTTextBoxFor(model => model.NotificationTemplates[i].MobileCC, false) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].EmailCC, "List Emails CC ',' seperated") 
       @Html.HTTextBoxFor(model => model.NotificationTemplates[i].EmailCC, false) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].MessageHeader, "Message Header Email") 
       @Html.HTTextBoxFor(model => model.NotificationTemplates[i].MessageHeader, false) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].MessageBody, "Message Body Email") 

       @Html.HTTextEditorFor(model => model.NotificationTemplates[i].MessageBody) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].SMSBody, "SMS Body") 
       @Html.HTTextAreaFor(model => model.NotificationTemplates[i].SMSBody) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].PushNotificationBody, "Push Notification Body") 
       @Html.HTTextAreaFor(model => model.NotificationTemplates[i].PushNotificationBody) 
      </div> 
      <p class="lead"> 
       Arabic Content 
      </p> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].MessageHeaderAr, "Message Header Email Arabic") 
       @Html.HTTextBoxFor(model => model.NotificationTemplates[i].MessageHeaderAr, false) 
      </div> 
      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].MessageBodyAr, "Message Body Email Arabic") 
       @Html.HTTextEditorFor(model => model.NotificationTemplates[i].MessageBodyAr) 
      </div> 

      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].SMSBodyAr, "SMS Body Arabic") 
       @Html.HTTextAreaFor(model => model.NotificationTemplates[i].SMSBodyAr) 
      </div> 

      <div class="col-sm-12"> 
       @Html.HTLabelFor(model => model.NotificationTemplates[i].PushNotificationBodyAr, "Push Notification Arabic") 

       @Html.HTTextAreaFor(model => model.NotificationTemplates[i].PushNotificationBodyAr) 
      </div> 
     </p> 
    </div> 
} 

Le binded HTML génère un contrôle avec des noms ID dans le nom Diffrent parce qu'il est tableau

donc, ce code fonctionne bien, mais je veux factoriser ce code en vue partielle comme celui-ci

@model BusinessLogic.Settings.NotificationTemplate 

<div class="tab-pane @Model.ActiveCSSClass" id="@string.Format(@Model.NotificationTypeString)"> 
    <p class="lead"> 
    </p> 
    @Html.HiddenFor(model => model.NotificationType) 
    <p> 

     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.ParametersString, "Parameters") 
      @Html.HTTextBoxFor(model => model.ParametersString, false, true) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.IsStopped, "Stop sending") 
      @Html.HTCheckedFor(model => model.IsStopped, "Stop sending", true) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.IsLogEnabled, "Log Notification") 
      @Html.HTCheckedFor(model => model.IsLogEnabled, "Log Notification", true) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.SendingNotificationType, "Sending Options") 
      @Html.HTMultiListFor(model => model.SendingNotificationType, 
        FillListHelper.GetList<SendingNotificationType_s_WS, SendingNotificationType_s>(), true) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.MobileCC, "List mobile CC ',' seperated") 
      @Html.HTTextBoxFor(model => model.MobileCC, false) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.EmailCC, "List Emails CC ',' seperated") 
      @Html.HTTextBoxFor(model => model.EmailCC, false) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.MessageHeader, "Message Header Email") 
      @Html.HTTextBoxFor(model => model.MessageHeader, false) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.MessageBody, "Message Body Email") 

      @Html.HTTextEditorFor(model => model.MessageBody) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.SMSBody, "SMS Body") 
      @Html.HTTextAreaFor(model => model.SMSBody) 
     </div> 
     <div class="col-sm-12"> 
      @Html.HTLabelFor(model => model.PushNotificationBody, "Push Notification Body") 
      @Html.HTTextAreaFor(model => model.PushNotificationBody) 
     </div> 
    <p class="lead"> 
     Arabic Content 
    </p> 
    <div class="col-sm-12"> 
     @Html.HTLabelFor(model => model.MessageHeaderAr, "Message Header Email Arabic") 
     @Html.HTTextBoxFor(model => model.MessageHeaderAr, false) 
    </div> 
    <div class="col-sm-12"> 
     @Html.HTLabelFor(model => model.MessageBodyAr, "Message Body Email Arabic") 
     @Html.HTTextEditorFor(model => model.MessageBodyAr) 
    </div> 

    <div class="col-sm-12"> 
     @Html.HTLabelFor(model => model.SMSBodyAr, "SMS Body Arabic") 
     @Html.HTTextAreaFor(model => model.SMSBodyAr) 
    </div> 

    <div class="col-sm-12"> 
     @Html.HTLabelFor(model => model.PushNotificationBodyAr, "Push Notification Arabic") 

     @Html.HTTextAreaFor(model => model.PushNotificationBodyAr) 
    </div> 
    </p> 
</div> 

Mais les noms des contrôles HTML si binded un tableau de la même objection NotificaitonTemplate classe a le même ID et le nom afin qu'il ne fonctionnera pas

Comment puis-je résoudre ce

+0

Vous faites de votre part un 'EditorTemplate' (en le nommant' NotificationTemplate.cshtml' et en le plaçant dans le dossier '/ Views/Shared/EditorTemplates /') et puis utilisez '@ Html.EditorFor (m => m.yourCollectionProperty)' pour générer le code html correct pour chaque élément de 'yourCollectionProperty' –

+0

[Que sont les modèles d'affichage et d'édition?] (https://www.exceptionnotfound.net/ asp-net-mvc-démystifié-affichage-et-éditeur-modèles /) – adiga

+0

Merci youuuuuuuuuuuuuuuuuuuuuuu –

Répondre

0

La réponse était sur le Commentaire de Stephen Muecke Merci beaucoup

Vous faites votre partie un EditorTemplate (en le nommant NotificationTemplate.cshtml et en le plaçant dans le répertoire/Vues/Shared/EditorTemplates/dossier), puis utilisez @ Html.EditorFor (m => m.yourCollect ionProperty) pour générer le html correct pour chaque élément de yourCollectionProperty