Tag: asp.net mvc

MVC 2 RC 2中的IValueProvider

我一直在使用MVC 2,似乎在某些时候,ModelBindingContext.ValueProvider类已被删除并替换为IValueProvider。 因为这个我在迭代ValueProvider.Keys时遇到了麻烦。 这是一个示例,我从代码中收到的消息已完成 private void foo(ModelBindingContext myMBC) { var myImportantKeys = myMBC.ValueProvider.Keys.where(keyValue => keyValue.StartsWith(“important”, StringComparison.InvariantCulture); foreach(var importantKey in myImportantKeys) { } } 我收到的消息是System.Web.MVC.IValueProvider不包含Keys的定义。 有人可以告诉我如何解决这个问题。

在MVC中声明html助手时,如何用破折号创建html属性?

例如,当我声明和Html.TextboxFor帮助器时,我如何创建data-bind属性? 简单地做: @Html.TextBoxFor(model => model.SomeProperty, new { data-bind=”something” }) 由于带有短划线“ – ”符号的命名问题,这是不合法的。 有没有办法解决这个问题,或者只是不可能传递名称包含破折号的html属性? 注意:我尝试拍打@ (这有助于你想在属性前面传递一个与C#保留字匹配的属性,比如“class”),但是没有做到这一点……

Html.OpenIdSelectorScripts抛出NullReferenceException的helper方法

尝试导航到LogOn页面时,我不断收到此错误: System.NullReferenceException:未将对象引用设置为对象的实例 抛出错误的代码行是: . 有谁知道为什么这行代码会抛出错误?

如何在mvc2中的html.label助手中设置ID和Text

我想在mvc2中的html.label帮助器中设置ID和Text属性 <%:html.label%> Plz帮我解决..

ASP.Net MVC 5子目录捆绑问题

我在ASP.Net MVC 5项目中看到了捆绑的奇怪行为。 当我在BundleConfig.cs文件中显式声明所有文件时,我的项目工作得很好,如下所示: bundles.Add(new ScriptBundle(“~/bundles/app”).Include( “~/app/app.js”, “~/app/config.js”, “~/app/dir1/file1.js”, “~/app/dir1/subdir1/file2.js”, ….. 但是,如果我切换到使用IncludeDirectory ,则开发期间的脚本路径( BundleTable.EnableOptimizations = false )不完整。 这就是我所看到的: bundles.Add(new ScriptBundle(“~/bundles/app”).Include( “~/app/app.js”, “~/app/config.js”) .IncludeDirectory(“~/app/dir1”, “*.js”, true) 当Chrome尝试获取file2.js时,Chrome会向我显示404。 捆绑系统将以下内容添加到我的布局页面: file2.js的路径是错误的。 它省略了路径的subdir1部分。 我在这里错过了什么吗?

从模型中获取数据注释属性

我想创建自定义客户端validation器,但我想通过业务逻辑层的Data Annotations属性定义validation规则。 如何在运行时访问模型validation属性? 我想写’generator’,它会转换这段代码: public class LoginModel { [Required] [MinLength(3)] public string UserName { get; set; } [Required] public string Password { get; set; } } 进入这一个: var loginViewModel= { UserName: ko.observable().extend({ minLength: 3, required: true }), Password: ko.observable().extend({ required: true }) }; 但当然不是来自.cs来源。 =) 也许反思? UPD 我发现了这个方法: MSDN 。 但无法理解如何使用它。

entity framework – 如何在种子方法中获取相对文件路径

为什么filePath为null? 有关如何获取相对filePath的任何想法? internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { // code here is not relevant to question } protected override void Seed(MvcProject.Models.FileDb context) { string filePath = System.Web.HttpContext.Current.Server.MapPath(“~/Content/File.txt”); // read File.txt using filePath and update the database } } 我在ASP .NET MVC项目上设置entity framework时创建的Migrations文件夹中的Configuration.cs文件中有上面的代码 当我在程序包管理器控制台中运行“Update-Database -Verbose”时,我收到一个错误,即filePath为null。 如果我手动设置filePath与文件的绝对URL: string filePath = “C:/Users/User1/My Documents/Visual Studio […]

获取堆栈跟踪中的参数值

我无法再现我们在错误日志中看到的一些错误。 如果我知道特定方法在抛出exception时使用了哪个记录ID,那么可以更轻松。 我们所有未处理的exception都由我们的全局exception处理程序处理,该处理程序将exception的所有细节以及HTTP请求的所有细节放入日志表中。 有没有办法捕获引发exception的方法的所有参数的值? 或者甚至更好,堆栈跟踪中的所有值?

将数据从“部分视图”传递到其父视图

如果我有视图和部分视图,有什么方法可以将数据从部分视图传递给父视图? 所以,如果我有View.cshtml : @Html.Partial(“_PartialView”) 和_PartialView.cshtml : @{ someDataFromPartialSomehow = “some-data” } Some content 我将如何实现这样的事情? 我尝试使用ViewBag.SomeDataFromPartialSomehow ,但这只会在Parent中导致null 。 一次尝试 要尝试解决在调用之前生成的数据问题,我试过这个: View.cshtml : @{ var result = Html.Partial(“_PartialView”); } @result _PartialView.cshtml : @{ ViewData[“Stuff”] = “foo”; } Content 但是对@ViewDate[“Stuff”]的调用仍然没有遗憾。

在web api中传递Dictionary 参数

我正在尝试将Dictionary对象作为参数传递给我的web api方法,但如果我检查日志文件,它总是会计数为0: Web api方法: [HttpPost] [ActionName(“SendPost”)] public void SendPost([FromBody] Dictionary values) { using (var sw = new StreamWriter(“F:\\PostTest.txt”, true)) { sw.WriteLine(“Number of items in the dictionary – ” + values.Count); } } 调用web api的逻辑: public HttpResponseMessage Send(string uri, string value) { HttpResponseMessage responseMessage = null; using (var client = new HttpClient()) { client.BaseAddress = new […]