Tag: asp.net mvc 4

将数组作为JSON发布到MVC控制器

我一直在努力寻找这个问题的解决方案。 在我的代码中,我正在构建一个对象数组; var $marks = []; var mark = function ( x, y, counter ){ this.x = x; this.y = y; this.counter = counter; } $marks.push(new mark(1, 2, 0)); $marks.push(new mark(1, 2, 1)); $marks.push(new mark(1, 2, 2)); 现在我想将这些数据发布到MVC控制器,所以我认为控制器中的数据类型将是List Marks或Marks数组。 为了发布数据,我试过了; var json = JSON.stringify($marks); $.post(‘url’, json).done(function(data){ /* handle */ }); 要么 var json = { […]

为什么在MVC4 Razor View中找不到扩展方法?

给出以下字符串扩展方法 namespace JHS.ExtensionMethods { public static class StringExtensions { public static string ToUSAPhone(this String str) { return String.Format(“{0:(###) ###-####}”, Double.Parse(str)); } } } @using语句被添加到MVC4 Razor视图中 @using JHS.ExtensionMethods; 以下字符串值调用扩展方法 @Model.producer.phone.ToUSAPhone() 这会导致以下错误 ‘string’ does not contain a definition for ‘ToUSAPhone’ 我还尝试将命名空间放在/ Views文件夹的web.config中并收到相同的错误。 我通过在C#类中调用相同的调用来validation扩展方法的工作原理 string test=producer.phone.ToUSAPhone(); 看来扩展方法的引用在MVC4 Razor视图中不可用,但我无法弄清楚为什么?

在Visual Studio 2012中添加对其他项目的引用

我有一个包含4个项目的解决方案。 Test.Common Test.Controller(Web项目的控制器)Test.Data(EntityFramework)Test.Web(MVC4) 当我将我的数据层的引用添加到我的Controller项目并浏览到我的Data项目目录时,我应该使用’bin / Debug’目录或’obj’目录中的dll吗? 而且,它有所作为吗?

MVC4将模型绑定到ICollection或List in partial

给定一个模型 public class Task { public int TaskId { get; set; } public string Title { get; set; } public ICollection Information { get; set; } } 哪里 public class SomeData { public int SomeDataId { get; set; } public string Description { get; set; } } 我有一个观点 @model myProject.Models.Task @Html.LabelFor(model => model.Title) @Html.Partial(“_InformationEdit”, Model.Information.ToList(), […]

LINQ表达式转换/从Int到String的Concat

我想用Concat两个表达式作为最终表达式 Expression<Func> 所以我创建了表达式belwo代码只适用于字符串类型,如果我将memberExpression作为Int32或DateTime抛出exception 类型’System.Int32’的表达式不能用于方法’System.String Concat(System.String,System.String)’的’System.String’类型的参数 如果我将表达式转换为 var conversion = Expression.Convert(memberExpression, typeof (string)); 获取没有强制运算符是在类型’System.Int32’和’System.String’之间定义的。 请帮我解决 码 MethodInfo bodyContactMethod = typeof (string).GetMethod(“Concat”,new[] {typeof (string), typeof (string)}); ParameterExpression parameter = Expression.Parameter(typeof (T)); body = Expression.Call(bodyContactMethod, cons, memberExpression); return Expression.Lambda<Func>(body, parameter);

Angular $ http错误响应statusText始终未定义

我试图向用户返回一个自定义错误消息,让他们知道出现错误时出了什么问题,但我已经尝试了所有内容来显示消息,似乎没有任何东西正在捕获它。 这是我的角度代码: $scope.save = function (style) { styleAPIservice.addStyle(style).success(function () { $scope.success = “Style added successfully”; }).error(function (data, status, headers, config, statusText) { $scope.error = “An error occurred while saving style: ” + data + “|”+data.data+”|”+data.statusText+”|”+statusText; }); } 这是它调用的styleAPIservice函数: styleAPI.addStyle = function (style) { return $http.post(AppRoot + “api/StyleAPI/”, JSON.stringify(style), { headers: { ‘Content-Type’: ‘application/json’ } }); […]

Azure WebSites或Azure云服务上的MVC4 API :’System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption’

当我尝试将我的Web API项目部署到Azure上的Cloud WebSites和Cloud Hosted Services时,我收到此错误: “{“ExceptionType”:”System.TypeLoadException”,”Message”:”Could not load type ‘System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption’ from assembly ‘System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’.”,”StackTrace”:”\r\nServer stack trace: \r\n at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)\r\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.Dispatcher.HttpControllerTypeCache.InitializeCache()\r\n at System.Lazy`1.CreateValue()\r\n\r\nException rethrown at [0]: \r\n at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)\r\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.Dispatcher.HttpControllerTypeCache.InitializeCache()\r\n […]

将Dictionary 传递给MVC Controller

我试图使用AJAX将javascript对象(键值对)传递给MVC Controller操作。 控制器操作具有接收对象的Dictionary参数。 [HttpPost] public ActionResult SearchProject(IDictionary filter … 当有问题的对象为空 (意味着它在javascript中的值为{})时,我在调试器中看到以下内容。 为什么控制器和操作名称会自动添加到Dictionary参数中? 使用fiddler我能够看到传递给我的控制器的内容,我没有看到传递这两个值。 如果javascript对象不为空,那么一切正常 我很难过..

AssemblyBinding BindingRedirect不适用于带有T4MVCExtensions的MVC4应用程序

我们有一个使用T4MVC的大型.NET Web应用程序MVC3。 我们最近将它升级到了MVC4,除了T4MVCExtensions(来自NuGet的版本3.6.5)仍然依赖于System.Web.Mvc版本3之外一切都很好。你应该在web.config中设置的运行时依赖项应该是应该的重定向程序集绑定,但它们似乎不是。 发生以下exception: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception […]

ASP.NET MVC中的动态路由操作名称

我想创建一个到以下URL的动态路由: http://localhost:51577/Item/AnyActionName/Id 请注意,控制器名称是静态的,不需要是动态的。 另一方面,我需要动作名称部分是动态的,这样无论写入URL的那一部分,我都会将用户重定向到Item控制器内的Index动作。 到目前为止我尝试的是: public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( “Items”, “Item/{action}/{id}”, new { controller = “Item”, action = “Index”, id = UrlParameter.Optional }); } 当我构建我的应用程序时,我收到以下错误: 无法找到该资源。 编辑 : 这是我的Global.asax文件和routeconfig.cs文件: public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } 这是RouteConfig.cs文件的内容,其中RouteConfig.cs @Nkosi提供的答案: public class RouteConfig { public […]