如何在同一页面上绑定一个 class 数组,但为一个对象提供相同索引 ID 的每组控件
How to bind one array of class on the same page but give each set of controls for one object same index ID
我需要从 MVC Razor 视图重构一些 HTML 代码并将其用作局部视图
@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>
}
Binded HTML 正在生成 ID 名称不同的控件,因为它是 Array
所以这段代码工作正常,但我想重构这段代码以像这样在局部视图中显示
@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>
但是 HTML 控件的名称是否绑定了相同对象 NotificaitonTemplate 的数组 Class 具有相同的 ID 和名称,因此它不会工作
我该如何解决这个问题
答案在 Stephen Muecke 的评论中
非常感谢
您将您的部分设为 EditorTemplate(通过将其命名为 NotificationTemplate.cshtml 并将其放置在 /Views/Shared/EditorTemplates/ 文件夹中)然后使用 @Html.EditorFor(m => m.yourCollectionProperty) 为 yourCollectionProperty
中的每个项目生成正确的 html
我需要从 MVC Razor 视图重构一些 HTML 代码并将其用作局部视图
@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>
}
Binded HTML 正在生成 ID 名称不同的控件,因为它是 Array
所以这段代码工作正常,但我想重构这段代码以像这样在局部视图中显示
@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>
但是 HTML 控件的名称是否绑定了相同对象 NotificaitonTemplate 的数组 Class 具有相同的 ID 和名称,因此它不会工作
我该如何解决这个问题
答案在 Stephen Muecke 的评论中 非常感谢
您将您的部分设为 EditorTemplate(通过将其命名为 NotificationTemplate.cshtml 并将其放置在 /Views/Shared/EditorTemplates/ 文件夹中)然后使用 @Html.EditorFor(m => m.yourCollectionProperty) 为 yourCollectionProperty
中的每个项目生成正确的 html