Tag: asp.net mvc 4

如何使用StructureMap v2.6解析signalR v2.0

在Global.asax的Application_Start中,我有以下内容 ObjectFactory.Initialize(cfg => { cfg.For().Singleton().Add (); }); 我的Hub接口是 public interface IDashboardHub { void Initialize(); } 我的中心如下: public class DashboardHub : Hub, IDashboardHub { private readonly ICpeAccountService _accountService; public DashboardHub(ICpeAccountService service) { _accountService = service; } [Authorize] public void Initialize() { Clients.All.UpdateStatus(“Hello World!!!”); } } 如果我删除注入的构造函数和解析器,那么我得到“Hello World”信号,JavaScript显示值。 如果我只是删除解析器,那么signalR不再找到无参数构造函数,并且不会调用Initialize方法。 如果我包含StructureMap依赖项解析器(它正在工作并立即注入大约40个其他类),那么我得到以下exception消息 StructureMap configuration failures: Error: 104 Source: Registry: […]

MVC4:使用chrome将.xlsx文件导出为mvc。 它适用于.xls但不适用于.xlsx

如何使用chrome通过mvc将.xlsx文件导出为ex​​cel。 它适用于.xls但不适用于.xlsx Response.ClearContent(); Response.AddHeader(“content-disposition”, “attachment; filename= Estimate1.xlsx”); Response.ContentType = “application/excel”; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); Response.Write(sw.ToString()); Response.End(); 当我打开excel文件。 它表现得像这样。 “Excel无法打开文件’FileName.xlsx’,因为文件格式或文件扩展名无效。” 非常感谢你的帮助! 🙂

如何从Visual Studio导航到其控制器的视图?

问题是解决方案资源管理器上有29个项目,项目同时包含asp.net mvc和asp.net web表单结构。 在MVC部分,Controller文件夹中有大约100个子文件夹,每个文件夹至少有3,4个控制器。 视图完全位于不同的位置[不在MVC文件夹中]。 我的第一个问题 [是否有任何工具可以显示解决方案资源管理器中整个文件的页面之间的关系。 第二个问题 [如何轻松地从控制器中获取控制器?]

如何使用html附件发送电子邮件

ASP.NET / Mono MVC4 C#应用程序。 html文档需要通过邮件作为附件发送。 我试过了 using (var message = new MailMessage(“from@somebody.com”, “to@somebody.com”, “test”, “Invoice 1>” )) { message.IsBodyHtml = true; var client = new SmtpClient(); client.Send(message); } 但是html内容出现在邮件正文中。 如何强制html内容显示为电子邮件附件? 更新 我尝试过exception回答,但文档仍然只出现在邮件正文的Windows Mail中。 消息源显示它包含两部分: —-boundary_0_763719bf-538c-4a37-a4fc-e4d26189b18b Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: base64 和 —-boundary_0_763719bf-538c-4a37-a4fc-e4d26189b18b Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: base64 两个部分都具有相同的base64内容。 如何强制html作为附件出现? 邮件正文可以为空。

ASP.NET MVC 5.1 EditorFor和Display不使用自定义模板

突然我的MVC应用程序停止使用我自己的自定义EditorFor或DisplayFor模板。 因为我一直在改变UI,所以我不确定它到底是什么时候失败了。 我将模板放在Shared文件夹下的DisplayTemplates和EditorTemplates 。 我用这个覆盖Global.asax的ViewEnginesCollection : ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new CSHtmlRazorViewEngine { PartialViewLocationFormats = new string[] { “~/Views/Shared/EditorTemplates/{0}.cshtml”, “~/Views/Shared/Partials/{0}.cshtml” } }); CSHtmlRazorViewEngine是: public sealed class CSHtmlRazorViewEngine : RazorViewEngine { public CSHtmlRazorViewEngine() : base() { this.AreaViewLocationFormats = new string[2] { “~/Areas/{2}/Views/{1}/{0}.cshtml”, “~/Areas/{2}/Views/Shared/{0}.cshtml” }; this.AreaMasterLocationFormats = new string[2] { “~/Areas/{2}/Views/{1}/{0}.cshtml”, “~/Areas/{2}/Views/Shared/{0}.cshtml” }; this.AreaPartialViewLocationFormats = new string[2] { “~/Areas/{2}/Views/{1}/{0}.cshtml”, “~/Areas/{2}/Views/Shared/{0}.cshtml” […]

在mvc ef中为显示名称动态选择两个标签

我正在使用Entity Framework,MVC。 如何在一个数据字段的两个标签之间动态更改(基于从另一个数据库检索的有关同一车辆的数据)? 理想情况下,我希望我的Model类中有这样的东西(这是伪代码,不希望编译): [Display(Name = “resCatalyst”, ResourceType = typeof(VehiclesModelResource))] public string Catalyst { get; set; } … public void SetElementDisplayName(bool DieselOlderThan2009) { if (DieselOlderThan2009) { Catalyst.SetDisplayName(Name = “resNMHCCatalyst”, ResourceType = typeof(VehiclesModelResource)); } else { Catalyst.SetDisplayName(Name = “resCatalyst”, ResourceType = typeof(VehiclesModelResource)); } } 我确实想过改变cshtml文件中的标签,如下所示: @if(Model.vltDataOne.FuelType == “D” && Model.vltDataOne.VehicleYear >= 2009) { @Html.LabelFor(model => model.testObd.Catalyst, […]

EF每次引用一个时都会创建一个新的UserProfile?

我的问题是,以下代码不仅创建了ProtectedPassword(应该如此),而且还创建了插入其中的所有者,即使该所有者已存在。 UserProfile user = PublicUtility.GetAccount(User.Identity.Name); //gets an existing UserProfile ProtectedPassword pw = new ProtectedPassword(model.Name, user, model.Password); ProtectedPassword.Create(pw); 因此,在创建新的ProtectedPassword之后,我最终得到了一个新的UserProfile(具有与前一个相同的值,除了新ID)。 我已经在这个问题上乱了几个小时了,如果有人可以帮助我,我会很感激! 顺便说一句,我使用ASP.NET MVC4和EF Code First。 首先,实体:ProtectedPassword: [Table(“ProtectedPassword”)] public class ProtectedPassword : ProtectedProperty { [Required] [MinLength(3)] [MaxLength(20)] public string Password { get; set; } private ProtectedPassword() { } public ProtectedPassword(string name, UserProfile owner, string password) { Name = […]

数据注释未显示MVC 4中的部分实体类

我已经看到了几十个关于如何通过部分类将元数据注释添加到通过entity framework生成的类的解释, 数据库优先 。 有人能告诉我为什么这些新的显示值没有显示在我的视图中吗? 这两个都是与我的entity framework生成的类相同的命名空间的一部分。 [MetadataType(typeof(xRef_CodesMetadata))] public partial class xRef_Codes { } public class xRef_CodesMetadata { public int CodeID { get; set; } public int CTB_ID { get; set; } [Required(ErrorMessage = “Please type a name”)] [Display(Name = “Code Name”)] [Column(TypeName = “Code Name”)] public string CodeName { get; set; } [Required(ErrorMessage = […]

如何在MVC4 RC WebApi中注册自定义模型绑定器

我最近从MVC4 beta更新到RC,并遇到了一个WebApi项目的小问题。 我注意到的第一件事就是删除了ServiceResolver。 在删除之前,我使用它以下列方式注册自定义模型绑定程序提供程序: IEnumerable modelBinderProviderServices = GlobalConfiguration.Configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider)); List services = new List(modelBinderProviderServices) { new CustomDynamicObjectModelBinderProvider() }; GlobalConfiguration.Configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray()); 利用此模型绑定程序提供程序的操作具有以下签名: [HttpPut] public void Put(CustomDynamicObject model) 我尝试用以下代码替换旧代码,但没有结果: GlobalConfiguration.Configuration.Services.Add(typeof(ModelBinderProvider), new CustomDynamicObjectModelBinderProvider()); 当我尝试将数据PUT到给定的操作时,不调用模型提供程序的GetBinder方法,并且模型参数设置为null。 通过将Acion /方法的签名更改为以下内容,我能够使用ModelBinder属性使操作使用想要的模型绑定器 [HttpPut] public void Put([ModelBinder(typeof(CustomDynamicObjectModelBinderProvider))] CustomDynamicObject model) 虽然这有效但我真的不想在我的所有控制器/动作中使用这种语法。 我想我应该提一下,我的模型绑定器提供程序inheritance自: System.Web.Http.ModelBinding.ModelBinderProvider 我这样说是因为我看到以下命名空间中有另一个ModelBinderProvider类: Microsoft.Web.Mvc.ModelBinding 回顾一下:如何在MVC4 RC WebApi中注册自定义模型绑定器?

获取具有条件的子实体的实体对象(使用Dynamic Linq)

这个问题是Dynamic Lambda Expression中的Getting Count()属性的延续 我问过我们是否可以将Count()方法放在动态lambda表达式中。 我可以使用Dynamic Expression API来完成它。 答案由 – xmojmr提供 但是最近我不得不实现一个Dynamic Lambda Expression来获得带有过滤数据的子实体的结果。 细节 : 我有一个父实体与其子实体链接。 现在,当我从父实体获取数据时,它也会返回包含其子数据的数据集合。 根据我之前的要求,我必须计算每个父记录返回的所有子记录。 我用它做了 – ParameterExpression cParam = Expression.Parameter(typeof(CPNDBase), “c”); NewExpression newExp = Expression.New(typeof(DTDataModel)); List bindings = new List(); MemberInfo memberInfo = typeof(DTDataModel).GetMember(“FILE_COUNT”)[0]; Dictionary paramExSymbols = new Dictionary(); paramExSymbols.Add(“c”, cParam); Expression expression = System.Linq.Dynamic.DynamicExpression.Parse(null, “c.CPNDocs.Count()”, paramExSymbols); MemberBinding memberBinding = […]