我认为这是一个简单的问题,但对于我的生活,我无法弄明白。我有一个从多行文本框导入许多电子邮件的表单。从该导入,我生成一个报告,告诉用户导入的电子邮件的状态和任何错误。我可以很好地生成报告数据,但似乎无法显示"错误"在视图中正确显示字段。问题似乎是" Errors"的编辑器模板。字段,没有被使用。我的问题是,如何让视图使用并呈现"错误"的编辑器模板。领域。我尝试过使用UIHint,但它没有用。任何帮助将不胜感激。
ViewModels.cs
//**************************Report View Models***************************************
public class ShowErrors
{
public string EmailAddress { get; set; }
public string ErrorDescription { get; set; }
}
public class ReportViewModel
{
public int TotalEmails { get; set; }
public int SuccessEmails { get; set; }
public int DuplicateEmails { get; set; }
public int InvalidEmails { get; set; }
[UIHint("ShowErrors")]
public virtual ICollection<ShowErrors> Errors { get; set; }
}
public static class ViewModelHelpers
{
//*************************ReportViewModel Helpers**************************************
public static ShowErrors ShowErrors_ViewModel_To_Domain(this ShowErrors item)
{
var showErrors = new ShowErrors
{
EmailAddress = item.EmailAddress,
ErrorDescription = item.ErrorDescription
};
return (showErrors);
}
}
报告控制器:(不要认为问题出在这里,我可以为报告生成数据,只是无法正确显示。
public ActionResult Import_Report(EmailEditViewModel emailEditViewModel)
{
string emailAddress = null;
string[] emailArray = Request.Form["ImportEmails"].Split (new string[] { System.Environment.NewLine }, StringSplitOptions.None);
var entityModified = new EmailContact();
var reportViewModel = new ReportViewModel();
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); //Regular Expression that checks email address is valid
List<ShowErrors> errorList = new List<ShowErrors>();
foreach (string item in emailArray)
{
ShowErrors errorItem = new ShowErrors();
emailAddress = item.Trim();
reportViewModel.TotalEmails += 1;
Match match = regex.Match(emailAddress); //Check email is in valid format
if (emailAddress == null || emailAddress == "")
{
reportViewModel.TotalEmails -= 1;
}
else if (!match.Success)
{
//Log this email as duplicate
reportViewModel.InvalidEmails += 1;
errorItem.EmailAddress = emailAddress;
errorItem.ErrorDescription = "Invalid Email";
errorList.Add(errorItem);
}
else if (_globalClasses.IsDuplicateEmail(emailAddress) > 0)
{
//Log this email as duplicate
reportViewModel.DuplicateEmails += 1;
errorItem.EmailAddress = emailAddress;
errorItem.ErrorDescription = "Duplicate Email";
errorList.Add(errorItem);
}
else
{
entityModified.EmailAddress = emailAddress;
entityModified.ActiveCampaigns = entityModified.Campaigns.Count(); //Calculates how many campaigns a contact is a member
try
{
db.EmailContacts.Add(_crypto.EncryptAndSanitizeEmailContacts(entityModified));
db.SaveChanges();
reportViewModel.SuccessEmails += 1;
AddOrUpdateEmailContacts(entityModified, emailEditViewModel.Campaigns); //Saves campaigns selected to this contact
db.SaveChanges();
//return RedirectToAction("Contact_List");
}
catch { }
}
}
reportViewModel.Errors = errorList;
return View(reportViewModel);
}
以下是视图:
@model HoltsCA.ViewModels.ReportViewModel
@{
ViewBag.Title = "Import_Report";
Layout = "~/Views/Shared/_LayoutDashboard.cshtml";
}
<fieldset>
<legend></legend>
<div class="row col-md-6">
<div id="bootstrapTableHeader" class="row">
<div class="col-sm-12">
<h2 style="text-align:center; color:#fff; font-size:1.3em;">Import Contacts Report</h2>
</div>
</div>
<div id="bootstrapTableRow" class="row">
<div class="col-sm-7" style="text-align:right">Total Emails:</div>
<div class="col-sm-5" style="text-align:left">@Html.DisplayFor(model => model.TotalEmails)</div>
</div>
<div id="bootstrapTableRow" class="row">
<div class="col-sm-7" style="text-align:right">Success Imported Emails:</div>
<div class="col-sm-5" style="text-align:left">@Html.DisplayFor(model => model.SuccessEmails)</div>
</div>
<div id="bootstrapTableRow" class="row">
<div class="col-sm-7" style="text-align:right">Duplicate Emails:</div>
<div class="col-sm-5" style="text-align:left">@Html.DisplayFor(model => model.DuplicateEmails)</div>
</div>
<div id="bootstrapTableRow" class="row">
<div class="col-sm-7" style="text-align:right">Invalid Emails:</div>
<div class="col-sm-5" style="text-align:left">@Html.DisplayFor(model => model.InvalidEmails)</div>
</div>
<div class="row" style="height:50px"></div>
<div id="bootstrapTableHeader" class="row">
<div class="col-sm-12">
<h2 style="text-align:center; color:#fff; font-size:1.3em;">Error Report</h2>
</div>
</div>
<div id="bootstrapAccentRow" class="row">
<div class="col-sm-6" style="text-align:left">
<b>Email Address</b>
</div>
<div class="col-sm-6" style="text-align:left">
<b>Error Description</b>
</div>
</div>
<div id="bootstrapRow" class="row">
@Html.DisplayFor(model => model.Errors)
</div>
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
最后这是编辑模板ShowErrors.cshtml
@model ShowErrors
@using HoltsCA.ViewModels
<fieldset>
<div class="col-md-6" style="text-align:left;">
@Html.DisplayFor(model => model.EmailAddress)
</div>
<div class="col-md-6" style="text-align:left;">
@Html.DisplayFor(model => model.ErrorDescription)
</div>
</fieldset>
答案 0 :(得分:0)
我认为您正在尝试使用编辑器模板显示错误。将您的错误模板放在共享文件夹下的DisplayTemplates文件夹中。
如果您使用@Html.DisplayFor
,则在显示编辑模板时,您的模板应位于DisplayTemplates文件夹和类似逻辑中。要指定要使用的模板,您也可以
@Html.DisplayFor(model => model.Errors, "ShowErrors")
另一件事是你的模型。错误是List<ShowErrors>
,你的显示模板里面只有@model ShowErrors
,你应该像IEnumerable<ShowErrors>
一样显示它,并在模板中迭代以显示所有错误。
@model IEnumerable<ShowErrors>
@using HoltsCA.ViewModels
@foreach(var error in Model)
{
<div class="col-md-6" style="text-align:left;">
@Html.DisplayFor(error => error.EmailAddress)
</div>
<div class="col-md-6" style="text-align:left;">
@Html.DisplayFor(error => error.ErrorDescription)
</div>
}
答案 1 :(得分:0)
那不是EditorTemplate
,而是DisplayTemplate
。
您只需要创建一个名为&#34; DisplayTemplates&#34;的文件夹。在Views/Shared
下,将ShowErrors.cshtml
部分视图放在文件夹中。
答案 2 :(得分:0)
您的模型是List<ShowErrors>
而不是一个ShowError
对象。
您可以将视图更改为:
@model List<ShowErrors>
@using HoltsCA.ViewModels
@foreach(var error in Model)
{
<fieldset>
<div class="col-md-6" style="text-align:left;">
@Html.DisplayFor(error => error .EmailAddress)
</div>
<div class="col-md-6" style="text-align:left;">
@Html.DisplayFor(error => error .ErrorDescription)
</div>
</fieldset>