Tag: asp.net mvc

ASP.NET Web API和类

我有一个标有[Serializable]的类。 当我从Web API返回它时,字段名称都很时髦。 通常返回的JSON是 [{“OrderId”:797 … 使用[Serializable]时返回JSON [{“k__BackingField”:797 … 我不想将其标记为可序列化以使用BinaryFormatter进行缓存。 除了编写自定义序列化程序或制作一个不可序列化的双胞胎类并编写猴子代码以在两者之间“施放”之外,还有其他方法吗?

如何将参数传递给ASP.NET MVC 2中的自定义ActionFilter?

我正在尝试创建一个自定义ActionFilter,它操作一组参数,这些参数将从控制器传递给它。 到目前为止,我的客户ActionFilter看起来像这样: public class CheckLoggedIn : ActionFilterAttribute { public IGenesisRepository gr { get; set; } public Guid memberGuid { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { Member thisMember = gr.GetActiveMember(memberGuid); Member bottomMember = gr.GetMemberOnBottom(); if (thisMember.Role.Tier <= bottomMember.Role.Tier) { filterContext .HttpContext .Response .RedirectToRoute(new { controller = "Member", action = "Login" }); } base.OnActionExecuting(filterContext); […]

渲染的局部视图与模型不匹配

所以我编写了一些代码,允许使用AJAX在ASP.NET MVC中动态添加和删除集合中的元素。 向集合添加新项目按预期工作,但删除不会。 模型集合按预期更新(通过索引删除相应的项目),但呈现的HTML始终显示已删除最后一个项目(而不是指定索引处的项目)。 例如,假设我有以下项目: 富 酒吧 巴兹 当我点击名为“Foo”的项目旁边的“删除”时,我希望生成的呈现HTML看起来如下: 酒吧 巴兹 当我通过控制器操作进行调试时,似乎就是这种情况,因为模型上的Names集合仅包含这些项目。 但是,返回给我的AJAX处理程序的呈现HTML是: 富 酒吧 我认为这个问题可能与缓存有关,但我没有尝试过(OutputCache指令,在$ .ajax中设置cache:false等)。 这是代码: DemoViewModel.cs namespace MvcPlayground.Models { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class DemoViewModel { public List Names { get; set; } public DemoViewModel() { Names = new List(); } } } DemoController.cs 这里明显的问题是RemoveName方法。 […]

用户很快就会退出

我正在使用ASP.NET身份会员资格。 这是Startup.Auth.cs代码: app.CreatePerOwinContext(EFDbContext.Create); app.CreatePerOwinContext(ApplicationUserManager.Create); app.CreatePerOwinContext(ApplicationSignInManager.Create); // Enable the application to use a cookie to store information for the signed in user // and to use a cookie to temporarily store information about a user logging in with a third party login provider // Configure the sign in cookie app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath […]

如何在ASP.NET MVC视图中对数据进行分组?

在Crystal Reports等报表工具中,有一些方法可以获取非规范化数据并将其按数据中的特定列分组,从而为指定列中的每个唯一项创建行标题。 如果我有这个: Category1 Data1 Category1 Data2 Category1 Data3 Category2 Data4 Category2 Data5 Category2 Data6 报告软件会将其分组如下: Category1 Data1 Data2 Date3 Category2 Data4 Data5 Data6 有没有办法在ASP.NET MVC视图中执行此操作,可能使用简单的linq短语或linq扩展方法与foreach或嵌套的foreach?

如何在MVC模型中表示一个月的复选框

我无法理解如何使用MVC创建下表,并成功将其绑定到模型: 我基本上需要跟踪一个事件需要发生的月份的哪几天。 这是我对模型的尝试: 编辑 :这不是一个月,而是任意4周的周期 public class ScheduleViewModel { public int PatientId { get; set; } public List Schedules { get; set;} } public class Schedule { public int Week { get;set;} public Day Day { get;set;} public bool IsSelected { get;set;} } public enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } […]

从StructureMap获取的HttpContext上的Null User

好吧,我之前的问题/设置有太多的变量,所以我把它剥离到它的裸骨组件。 给出下面的代码使用StructureMap3 … //IoC setup For().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null )); For().Use(); //Classes used public class CurrentUser : ICurrentUser { public CurrentUser(HttpContextBase httpContext) { if (httpContext == null) return; if (httpContext.User == null) return; var user = httpContext.User; if (!user.Identity.IsAuthenticated) return; UserId = httpContext.User.GetIdentityId().GetValueOrDefault(); UserName = httpContext.User.Identity.Name; } […]

具有部分CRUD的MVC 5 BeginCollectionItem

我在下面对这个问题进行了修改,这个问题仍然是一样的,但希望通过模型以及我想要实现的目标以及我遇到问题的方式更加清晰。 下面显示了两个类,Company和Employee,Company有一个Employees列表。 这将是一个输入表单,因此开始时将没有数据。 最终,我希望用户能够根据需要向Company对象模型添加尽可能多的Employee对象,并更新Employee对象 我是否使用BeginCollectionItem在正确的轨道上,所以我可以添加/删除尽可能多的Employee对象? 当我单击Add按钮时,它会将其带到另一个页面上的部分视图(使用AjaxActionLink),但不能使用JavaScript。 更新删除了AjaxActionLink并改为使用JavaScript。 指数 @model MvcTest.Models.Company @{ ViewBag.Title = “Index”; Layout = “~/Views/Shared/_Layout.cshtml”; } Company @Html.LabelFor(m => m.Name) @Html.EditorFor(m => m.Name) Employees @foreach (var Employee in Model.Employees) { Html.RenderPartial(“_Employee”, Employee); } @section Scripts { $(‘#addemployee’).on(‘click’, function () { $.ajax({ async: false, url: ‘/Company/AddNewEmployee’ }).success(function (partialView) { $(‘#new-Employee’).append(partialView); }); }); } _Employee […]

带有Html Helper的条件html属性

我正在使用Html助手创建一个复选框。 在某些情况下,我想将disabled属性添加到htmlAttribute对象。 我有以下代码: @if (Model.IsAuthorized) { @Html.CheckBoxFor(x => @Model.Property, new { @class = “input-class” }) } else { @Html.CheckBoxFor(x => @Model.Property, new { @class = “input-class”, @disabled = “disabled” }) } 我想让这段代码更简洁。 有没有办法在一行中有条件地添加某些html属性/没有块条件?

存储库模式,POCO和业务实体

我知道存储库模式上已经存在很multithreading,但不知怎的,我觉得我的问题有点不同。 也许是因为昨天是我第一次听说POCO这个词。 我的问题是 – 通常,我在我的业务实体中添加和保存方法。 假设我正在编写Q / A网站,并且我有以下实体:问题,答案和评论。 如果我想使用存储库模式,我基本上只需要保留业务实体中的属性(例如,问题),并将我的操作移动到存储库类(例如,QuestionRepository),对吧? 如果这是真的,那么POCO是指具有属性的商业实体吗? 我正在使用Entity Framework 4.0,后者在edmx代码中创建了我的实体。 如果我想使用存储库模式,那么就不需要编写我自己的业务实体(问题,答案等),因为它们已经由EF生成了,对吧? 我需要的只是存储库来做CRUD? 我将为这个例子提供三个存储库,每个实体一个存储库?