Tag: asp.net mvc

在MVC之外使用ASP.Net MVC数据注释

我想知道是否有一种方法可以在没有MVC网站的情况下使用ASP.Net的数据注释。 我的例子是我有一个曾经创建过的类需要validation,否则会抛出错误。 我喜欢数据注释方法,而不是initaliser触发的一堆if块。 有没有办法让这个工作? 我以为它会是这样的: 添加数据注释 在初始化器中触发一个方法,该方法在类上调用MVCvalidation器 有任何想法吗? 我必须承认我还没有将MVC框架添加到我的项目中,因为我希望我可以使用数据注释类System.ComponentModel.DataValidation

ASP.NET MVC基于域和主机名的路由

我是ASP.NET MVC的新手,我的大多数经验都是在ASP.NET Webforms上。 有没有办法根据域/主机名设置路由,即www.domain.com到一个区域,admin.domain.com到另一个区域,www.differentdomain.com到web应用程序上的另一个区域。 此外,有没有办法做一个全能,即* .domain.com将路由到另一个区域?

MVC:Dictionary需要一个类型为’System.Collections.Generic.IEnumerable`1的模型项

我收到此错误,我不确定我是否能够这样做,这是我的代码.. 应用控制器 public ActionResult AppView() { List apps; using (ISiteDbContext context = _resolver.GetService()) { apps = context.Applications.ToList(); } return PartialView(“AppView”, apps.OrderBy(a => a.Name).ToList()); } 渲染部分 – 这是在家庭控制器中的视图内。 @{Html.RenderPartial(“~/Views/Application/AppView.cshtml”, new Example.Services.DAL.Application());} 和我的应用程序视图 @model IEnumerable @{ ViewBag.Title = “Applications”; } Applications @Html.ActionLink(“Add New Application”, “Create”) @Html.DisplayNameFor(model => model.Name) @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.Name) […]

‘System.Web.WebPages.Html.HtmlHelper’不包含’ActionLink的定义

首先 – 我已经搜索了SO并发现了许多问题同样的问题,尽管在我的web.config中添加并没有解决问题… 好的,所以我已经开始了一个基于模板Angular SPA的新项目 – 链接在这里: http://visualstudiogallery.msdn.microsoft.com/5af151b2-9ed2-4809-bfe8-27566bfe7d83 我已将MVC升级到版本5,将webApi升级到版本2 – 与解决方案中的其他项目一致。 现在,在SPA网站的_layout页面中,我尝试使用@Html.ActionLink帮助程序,但不断收到错误’System.Web.WebPages.Html.HtmlHelper’ does not contain a definition for ‘ActionLink 我确信这完全取决于一些web.config配置 – 我安装的软件包和模板配置中的设置之间存在某种冲突。 特别是这部分: 如果我将其更改为使用版本5,那么我会收到无法找到程序集(v5)的错误等。 关于如何解决这个冲突,我的想法已经不多了。 以下是我的SPA web.config中的配置部分 如果我将版本更改为5,我会收到以下错误: Assembly Load Trace: The following information can be helpful to determine why the assembly ‘System.Web.WebPages.Razor, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ could not be loaded. === Pre-bind state information === […]

模型中的MVC5 Razor NullReferenceException

出于某种原因,每当我尝试访问我的模型时,我都会收到NullReferenceException。 这是我的控制器的代码: public async Task Bar(string fooSlug, int barId) { var foo = await mediaService.GetFoo(fooSlug); var bar = await barService.GetBarFromFooByTitleId(foo.TitleId, barId); var viewModel = new ViewModels.BarViewModel(foo, bar); return View(viewModel); } ViewModel中的代码: public class BarViewModel { public Models.Sub.FooDetails Foo{ get; set; } public Models.Sub.BarDetails Bar { get; set; } public BarViewModel(Models.Sub.FooDetails foo, Models.Sub.BarDetails bar) { this.Foo […]

ASP.NET MVC重新编译限制达到了15个HostingEnvironment,启动了关闭HostingEnvironment导致关机

在某些时候,在代码推送后不久,我们看到在我们的Web应用程序中发生了大量重启,没有记录任何问题。 所以我发现这篇文章: http : //weblogs.asp.net/scottgu/433194并且我们添加了Application_End日志记录,它立即显示了这个: _shutDownMessage =重新编译限制达到了15个HostingEnvironment启动了关闭HostingEnvironment导致在系统的System.Environment.get.StackTrace()处的System.Environment.GetStackTrace(Exception e,Boolean needFileInfo)处关闭_shutDownStack =,系统的System.Web.Hosting.HostingEnvironment.InitiateShutdownInternal() System.Threading.ExedownDemand(在System.Web.HttpRuntime.ShutdownAppDomain(String stackTrace)上的System.Threading.ExedownCallBack(对象状态)System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调)中的.Web.Hosting.HostingEnvironment.InitiateShutdownWithoutDemand() System.Threading.ThreadPoolWorkQueue上的System.Threading.Quereading.QuereadingUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx)处的System对象状态,布尔值preserveSyncCtx) 。调度() 谷歌搜索这个错误显然很少,所以我们更新了我们的web.config 和中提琴! 一切都恢复正常。 我们彻底审查了我们的变化,但没有发现任何我们认为可能是罪魁祸首的事情。 有没有其他人遇到这个或类似的,或者知道/怀疑世界上有什么可能导致这种情况? 任何反馈都会很棒!

ASP.NET MVC中<%#和<%=有什么区别?

ASPX内联代码中的<%#和<%=有什么区别?

MVC 4 – 如何将模型数据传递到局部视图?

我正在构建一个配置文件页面,其中包含许多与特定模型(租户)相关的部分 – AboutMe,MyPreferences – 这些事情。 这些部分中的每一部分都将是部分视图,以允许使用AJAX进行部分页面更新。 当我点击TenantController中的ActionResult时,我能够创建一个强类型视图,并将模型数据传递给视图。 部分观点无法实现这一点。 我创建了一个局部视图_TenantDetailsPartial : @model LetLord.Models.Tenant @Html.LabelFor(x => x.UserName) // this displays UserName when not in IF @Html.DisplayFor(x => x.UserName) // this displays nothing 然后我有一个视图MyProfile将呈现提到的部分视图: @model LetLord.Models.Tenant @Html.Partial(“~/Views/Tenants/_TenantDetailsPartial.cshtml”, new ViewDataDictionary()) 如果我在@if(model != null){}内的@if(model != null){}中将代码包装在DIV中,那么页面上就不会显示任何内容,所以我猜测有一个空模型被传递给视图。 为什么当我从ActionResult创建一个强类型视图时,’session’中的用户被传递给视图? 如何将’session’中的用户传递给不是从ActionResult创建的局部视图? 如果我对这个概念遗漏了一些,请解释一下。

将字典传递给控制器​​asp.net mvc

我想通过Ajaxpost将类型的字典传递给我的控制器。 这里的主要原因是这里的post可能有1-3个键值对(这些值在编译时都不知道),并且将来可能会达到5。 同样在post中我必须传递一些其他数据,例如Id和name,这些数据都正常。 我将如何在javascript中构建这个词典,然后通过JQuerypost发送它,最后在控制器上接收它来处理? 编辑2:我决定用每个值的post解决这个问题而不是尝试传递字典。 编辑:这是我的function来源,所以你可以看到我在尝试: function BindAddMenuItem() { $(“.AddMenuItem”).click(function (e) { e.preventDefault(); //get header id from link by removing addmenuitem from this.id var currentId = $(this).attr(“id”).replace(“AddMenuItem”, “”); //get itemnumber, itemname, itemdetails from textboxes with same header id var restaurantId = jQuery.trim($(“#RestaurantId”).val()); var itemNumber = jQuery.trim($(“#ItemNumber” + currentId).val()); var itemName = jQuery.trim($(“#ItemName” + currentId).val()); var […]

授权不使用角色的属性

我在使用Authorize属性处理角色方面遇到了麻烦。 这就是我装饰我的控制器的方式: [Authorize(Roles = “admin”)] public ActionResult Index() { … } 这就是我记录用户的方式: string roles = “admin”; FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles ); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); HttpContext.Current.Response.Cookies.Add(cookie); 但我的用户仍被拒绝访问。 我哪里错了?