Tag: asp.net mvc

测试HtmlHelper时如何解决图像路径问题?

我在测试HTML帮助程序时遇到了一个问题。 基本上我正在创建一个包含大量行,列和不同类型数据的网格。 在标题中还有一个图像,用于通知用户数据按哪个列排序。 但是,当我现在正在编写我的测试时(太迟了,但迟到总比没有好?!),我得到了这个错误: “应用程序相对虚拟路径’〜/ Images / SortingArrowUp.png’不能成为绝对路径,因为应用程序的路径未知。” var imgPath = VirtualPathUtility.ToAbsolute(“~/Images/SortingArrowUp.png”); 我该怎么解决这个问题。 我可以理解在测试期间这可能是一个什么问题,并且图像可能不可用而且所有这些,但是这样做的正确方法是什么呢?

使用EF6加载数据库初始化程序时出错

我一直在努力学习这个教程… http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for -an-asp-net-mvc-application但我一直收到以下错误…… system.invalidoperationexception = {“无法为应用程序配置中指定的DbContext类型’WeddingPreparations.Dal.WeddingContext’设置’WeddingPreparations.Dal.WeddingInitializer,KevinLisaWedding’类型的数据库初始化程序。有关详细信息,请参阅内部exception。”} 内在的例外是…… {“无法从程序集’EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’加载类型’WeddingPreparations.Dal.WeddingContext’。”:“WeddingPreparations.Dal.WeddingContext”} 堆栈跟踪是…… at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) at System.Data.Entity.Internal.InitializerConfig.c__DisplayClass6.b__1(ContextElement e) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromEntityFrameworkSection(Type contextType) at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.c__DisplayClass1.b__0(Tuple`2 t) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, […]

entity framework存储过程 – 使用CodeFirst的多个结果集

我使用以下代码从存储过程中获取常规结果: var paramUserId = new SqlParameter { ParameterName = “userId”, Value = userId }; string query = string.Format(“{0} {1}”, “SpSetFoo”, “@userId”); var results = context.Database.SqlQuery(query, paramUserId); result = results.ToList(); 同时我需要从另一个存储过程中检索多个结果集,根据这个文档,我发现它是可能的: http : //msdn.microsoft.com/en-us/data/jj691402.aspx 但是,微软的例子是使用ADO.NET。 如果没有使用EF的ADO.NET,不可能实现相同的结果吗? 谢谢

如何让Html.CheckBoxFor()在字符串字段上工作?

我正在使用带有Razor和C#的ASP.NET MVC3。 我正在制作各种表单构建器,所以我有一个具有以下对象集合的模型: public class MyFormField { public string Name { get; set; } public string Value { get; set; } public MyFormType Type { get; set; } } MyFormType只是一个枚举,告诉我表单字段是复选框,文本框,文件上传,还是其他什么。 我的编辑器模板看起来像这样(请参阅注释): 〜/查看/ EditorTemplates / MyFormField.cshtml @model MyFormField @{ switch (Model.Type) { case MyFormType.Textbox: @Html.TextBoxFor(m => m.Value) case MyFormType.Checkbox: @Html.CheckBoxFor(m => m.Value) // This does not […]

如何在所有cshtml页面中添加“默认使用”?

我正在创建我的第一个MVC.Net应用程序,我发现自己包括@using Gideon.Core.Mvc; 在几乎每一页上。 是否可以将其默认添加到所有页面? 在Asp.Net中,我能够将控件的默认内容添加到web.config中,希望它也可以为MVC.Net完成。

如何根据实体输入参数过滤IEnumerable

我正在使用现在的entity framework – 但它是所有ORM甚至IEnumerable之间“共享”的问题。 假设我在MVC中有一个方法如下所示: [HttpPost] public ActionResult Foo(FooModel model) { var context = new Context(); — The EF session var data = context.Foo.Where(???).ToList(); return View(data); } 我想根据输入参数查询上下文,如: var data = context.Foo.Where(x => x.Date == model.Date && x.Name == model.Name && x.ItemCode = model.ItemCode).ToList(); 但它比这更复杂,因为如果上面的一个参数( Date \ Name \ ItemCode )为null,我不想将它包含在查询中。 如果我硬编码它看起来像这样: var query = […]

在Html.BeginForm MVC4控制器动作中传递多个参数

我有这样的事情: public ActionResult ImageReplace(int imgid,HttpPostedFileBase file) { string keyword = imgid.ToString(); ……. } 在我的.cshtml中: @model Models.MemberData @using (Html.BeginForm(“ImageReplace”, “Member”, FormMethod.Post, new { imgid = @Model.Id, enctype = “multipart/form-data” })) { } 这里imgid的值没有传递给控制器​​动作。 显示错误,参数字典包含参数’imgid’的非可空类型’System.Int32’的空条目,用于方法’System.Web.Mvc.ActionResult ImageReplace

JQuery DataTables ASP.NET MVC的库

我想在我的ASP.NET MVC 5项目中使用jQuery DataTables。 我确实使用了数据表并且它的工作完全正常,但问题是我不得不手动设置filter和查询,我感觉我试图在mvc中实现数据表的方式不太对。 是的我确实得到了结果,但我想遵循和标准加上我想要的东西,我不必一次又一次地输入filter和分页代码,我只发送参数到某个函数或类,我得到结果。 这就是为什么我试图寻找一些带有适当文档的数据表库。 当我遇到这个图书馆时。 https://github.com/ALMMa/datatables.mvc 但是没有好的文档可以让我理解该库中究竟发生了什么,或者如何使用该库? 是的我试过那个库,但是由于缺乏c#和asp.net的知识,我不明白如何实现它,我找不到任何与这个库相关的例子,我可以理解这个库的工作.. 然而,我也偶然发现了这个记录良好的过程。 http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part 它确实很有效,因为它们提供了如何实施的非常详细的解释。 我制作了我的控制器代码。 public ActionResult Index(jQueryDataTableParamModel param = null) { if (Request.IsAjaxRequest() && param != null) { var allCategories = _db.Categories.ToList(); IEnumerable categories; var sortColumnIndex = Convert.ToInt32(Request[“iSortCol_0”]); var sortDirection = Request[“sSortDir_0”]; // asc or desc Func orderingFunction = (c => sortColumnIndex==1? c.Name : […]

FormsAuthentication和WebSecurity之间的区别

我正在探索ASP.NET MVC的可能性,在Visual Studio的示例Web应用程序中,WebMatrix.WebData.WebSecurity用于成员资格(创建帐户,并指定用户登录以查看特定页面等)。 但经过一些搜索后,我发现还有一个可用于成员资格的System.Web.Security.FormsAuthentication类。 有谁知道这两个class级之间的差异/利弊? 何时使用WebSecurity以及何时使用FormsAuthentication? (也许是FormsAuthentication的一个明显例子) 提前致谢

将RDLC数据源设置为对象的问题

背景: 我的任务是将Access数据库应用程序转换为ASP.Net C#MVC。 这是我的第一个MVC应用程序。 有10份报告需要转换。 我们正在使用RDLC文件和reportviewer。 我正在使用Visual Studio 2010以及所有最新的补丁。 我们正在连接到已由现有Access数据库填充的SQL Server数据库,因此表结构几乎是一成不变的,否则我们将不得不尝试转换10年的数据。 我已完成除两份报告之外的所有报告。 最终报告需要更多处理,而不仅仅是显示数据库中的数据。 最简单的方法是创建一个C#对象并执行所有处理服务器端,然后使用RDLC显示结果。 问题 我遇到的问题是Visual Studio无法识别我创建的对象作为潜在的数据源。 每次我尝试“添加数据集”时,它都会显示“数据源配置向导”,并且只提供SQL Server数据库作为可能的数据连接。 我知道有一个屏幕存在,允许您选择一个对象作为您的数据集,但我从来没有看到该屏幕。 以下是对象(为清晰起见,删除了处理function): public class TurnAroundVal { // Registration Package information public string dataType { get; set; } // Calculated totals; values only set through constructor or calculation function public int packageCount { get; private set; } […]