Tag: asp.net mvc

entity frameworkDataContext关注 – 它是否在我的控制器中正确处理?

我已经交了一些代码,在控制器类中有一个属性,用于保存初始化的数据库上下文。 public class MyController: Controller { protected AssetManagerContext db = new AssetManagerContext(“ConnectionString”); // Actions…etc. [HttpGet] public ActionResult Edit(int id) { MyAsset myAsset = db.Assets.Find(id); // Used and not disposed return View(myAsset); } } 大多数操作使用此Context而不进行处理,我担心的是上下文保持开放。 我是否需要关注此上下文未被显式关闭(通过.Dispose()或using {}语句 如果我应该担心,我应该如何处理这种情况,因为变量是类的一部分并在操作中使用?

Sendgrid System.ArgumentException:未知元素:html

我刚刚将以下软件包更新到最新版本: a)SendGrid.SmtpApi已更新至1.3.1 b)SendGrid更新到6.0 突然间WebTransport.Deliver方法不再存在 – 没问题,我已经切换到DeliverAsync方法,但现在我得到一个非常奇怪的错误,它应该在2年前修复: System.ArgumentException:未知元素:html 这是可能感兴趣的堆栈跟踪的一部分: System.ArgumentException:未知元素:html 在SendGrid.ErrorChecker.CheckForErrors(HttpResponseMessage响应,流媒体流) 在SendGrid.ErrorChecker.d__0.MoveNext() —从抛出exception的先前位置开始的堆栈跟踪结束— 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在SendGrid.Web.d__0.MoveNext() —抛出exception的前一个位置的堆栈跟踪结束—在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 这也是我的电子邮件发送代码的样子: SendGridMessage emailMessage = new SendGridMessage(); emailMessage.AddTo(user.CompleteRegistrationModel.UserEmailAddress); emailMessage.From = new MailAddress(ConfigurationHelper.EmailSenderAddress, ConfigurationHelper.EmailSenderName); emailMessage.EnableTemplateEngine(ConfigurationHelper.ConfirmationEmailTemplateId); emailMessage.AddSubstitution(“-urlaplicatie-“, new List() { String.Format(“{2}{0}{1}”, user.CompleteRegistrationModel.CompanyUrlPrefix, ConfigurationHelper.DomainSuffix, ConfigurationHelper.ApplicationAccessProtocol) }); emailMessage.AddSubstitution(“-username-“, new List() {user.CompleteRegistrationModel.UserEmailAddress}); emailMessage.AddSubstitution(“-confirmationurl-“, new List() {user.CompleteRegistrationModel.UserEmailAddressConfirmationCompleteUrl}); emailMessage.Subject = String.Format(“{0}{1}”, user.CompleteRegistrationModel.CompanyUrlPrefix, ConfigurationHelper.DomainSuffix); emailMessage.Text = ” “; […]

无法加载文件或程序集’System.Web.Http.WebHost’

将MVC网页部署到我的IIS后,我收到以下错误: Could not load file or assembly ‘System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified. 我已完成以下更改以防止此问题: 为webhost设置Copy Local = True 从nuget添加了WebApi 2.2 System.Web.Http.WebHost存在于i bin文件夹中 我的web.config有以下dependentAssembly: 码: 我仍然得到错误,我做错了什么?

如何使用会话基于主页的下拉选择重定向用户?

我的主页上有一个下拉列表,其中包含部门列表。现在我有几个页面显示基于部门选择和更改的记录列表。 因此,当用户从下拉列表中选择新部门时,我会向我的MVC控制器发出AJAX调用,并将部门ID发送到该控制器并将其存储在会话中,并在所有页面中使用该会话,因为我的所有页面都是由部门驱动的。 执行: @{ int departmentId = 0; int.TryParse(Convert.ToString(Session[“departmentId”]), out departmentId); } //filling dropdown based on departmentId $(document).ready(function () { $(“#department”).change(function () { var departmentId = $(this).val(); $.ajax({ url: “@Url.Action(“ChangeDepartment”, “Home”)”, data: { id: departmentId }, success: function (e) { if (‘@departmentId’ > 0) { if (window.location.pathname == ‘/Employee/Skill’) { window.location.href = ‘/Employee/Skill’; } else […]

如何在html帮助器中使用asp.net mvc 3 razor进行内联样式

我想做这个 @Html.TextBoxFor(x => x.BackgroundColor, new { style = “width: 20px; background-color: @Model.BackgroundColor;” }) 但是它不会渲染我的Mode.Background颜色(在firebug中我只看到@ bModel.BackgroundColor“)。这可能吗?

System.MethodAccessException,CAS和Visual Studio调试器

我正在调用ModelMetadataProviders.Current.GetMetadataForProperties来使用Visual Studio 2010获取WPF应用程序中的ModelMetadata列表。此调用执行正常,我得到IEnumerable 作为回报。 在代码行,我尝试迭代这个,我得到System.MethodAccessException。 有趣的是,只有在我使用F5进行调试时才会发生这种情况。 如果我用Ctrl + F5运行代码,那么我不会得到这个exception,代码工作正常。 如果我从Windows资源管理器运行exe,它运行正常。 当然,我错过了一些东西。 可能是,System.Web.Mvc无法从WPF应用程序中使用? (为什么)有关visual studio调试器的东西吗? 任何帮助将不胜感激。 这是完整的例外: 尝试通过安全透明方法’System.Web.Mvc.TypeDescriptorHelper.Get(System.Type)’来访问安全关键方法’System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider..ctor(System.Type)’失败。 程序集’System.ComponentModel.DataAnnotations,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35’是有条件的APTCA程序集,在当前的AppDomain中未启用。 为了使该组件由部分信任或安全性的透明的代码中使用,请在创建应用程序域时添加组件名称“System.ComponentModel.DataAnnotations,公钥= 0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9”到的PartialTrustVisibleAssemblies列表。

在mvc 2中上传和预览图像

我通过转换asp.net网站来学习mvc 2.在我的页面上我必须上传图像并显示图像的预览。 我的asp.net页面的屏幕截图如下。 我已经将模型创建为 public class Contest { public int contestid { get; set; } public string ContestName { get; set; } public string Description { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public int UserId { get; set; } public bool IsActive { get; […]

asp.net mvc中的多文件上传

我需要从同一个表单中获取单个文件上传器和多个文件上传器的文件。 并且还需要知道这些文件来自哪个输入字段。 从Request.Files我可以获取所有文件,但无法知道这些文件来自哪个字段。 我有一张表格。 `

我想让EditFor文本框只接受数字

这是我的ViewModel: [Required(ErrorMessageResourceType=typeof(Resources.ValidationsResources), ErrorMessageResourceName = “Required”)] [Range(0, 9999, ErrorMessageResourceType = typeof(Resources.ValidationsResources), ErrorMessageResourceName = “SomenteNumeros”)] public Int16 Quantidade { get; set;} 这是我的观点: Quantidade @Html.EditorFor(model => model.Quantidade) 但我想阻止这个编辑器中的所有单词,只允许数字。 我怎样才能做到这一点?

asp.net MVC – 如何通过不同的存储库类共享同一个SqlConnection实例

我正在创建一个使用MVC5和普通ADO.NET的新项目(仅作为学习练习),我需要创建一个存储库来注册一个模型,该模型包含多个相关对象,这些对象也需要同时创建,并且这些对象在转弯可能需要插入其他物体。 我能想到的最简单的解决方案是有一个庞大的方法(在存储库中)接收父对象的实例(其中包含需要插入的所有相关对象),并且还有一个接收所有相关的存储过程数据作为表值参数,然后使用单个事务插入所有内容。 虽然这看起来似乎是最简单的方法,但我并不是它的忠实粉丝,所以我想知道的是,我是否可以使用任何方式/通用实践来共享为父对象创建的SqlConnection的相同实例与其他相关的对象? 我想也许在相关对象的构造函数中传递SqlConnection对象,这样每个存储库只需要处理插入单个对象的逻辑,但我不确定。 编辑 ——————————- 这是父对象(Model)的存储库,我认为它应该实例化SqlConnection并启动事务 public class ModelRepository : IModelRepository { public int Add(Model entity) { using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); using (var command = conn.CreateCommand()) { command.Transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted); command.CommandText = “up_Model_Insert”; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(command.CreateParameter(“@pName “, entity.Name)); command.Parameters.Add(command.CreateParameter(“@pDescription”, entity.Description)); //Other parameters… //Call the repositories of the other objects […]