Tag: asp.net mvc 3

c#User.IsInRole命名空间

注意,我正在使用c#MVC 3,我试图在一个类中使用它,而不是一个控制器。 我的程序顶部有以下内容 using System.Web.Security; 我尝试执行以下操作但收到消息: “User”这个名称在当前上下文中不存在。 这是我的部分代码: using System.Web.Security; …. …. if (User.IsInRole(“Admin”)) { } 我认为必须对命名空间做一些事情但是查看文档,我应该需要的是System.Web.Security。

如何根据以前的页面选择过滤Telerik Grid

我想做的是: – 索引页面将包含所述类别的下拉列表,选择1然后提交,重定向到telerik网格页面,其中所有记录都保存在所选类别的大表中。 因此,例如宠物商店,商店在下一页上有哪种类型的宠物的下拉,网格上填充了商店今天可用的所有类型的宠物。 已经将日期filter排序,因为它已应用于数据绑定。 数据库是通过edmx连接的,它有2个没有关系的表,但是有一个类别/宠物表,详细说明了每个类别/宠物,然后有一个记录表,其中有一个类别/宠物列,其中2表有单个字段incommon。 我一直试图通过使用ViewData来完成这项工作,这对于下拉列表非常合适,但无法填充网格下一页。 所以有点像http://demos.telerik.com/aspnet-mvc/grid/selectionserverside,但如果可能的话有一个下拉菜单和2页。

一劳永逸是什么是处理MVC中的错误,exception和404的最佳路由方法

SO和Web上有很多文章试图优雅地处理404和exception。 根据我的阅读,最好的建议似乎是404的路线像这样: routes.MapRoute( “404-PageNotFound”, “{*url}”, new { controller = “ErrorController”, action = “PageNotFound” } ); 然后,对于其他错误,在Controller上有HandleError属性,并在web.config中打开CustomErrors,因此它将转到error.cshtml页面。 但是我已经读过,如果你得到一个没有将HTTP代码设置为500的exception,那么HandleError将不起作用。 我们能否最终产生一个处理404 / Exceptions / ASP.Net错误的答案/最佳实践,我们可以将其应用于我们的所有项目? 谢谢

如何使用Foolproof的ModelAwareValidationAttribute进行不显眼的客户端validation

我正在尝试使用MVC Foolproofvalidation框架( http://foolproof.codeplex.com/ )但我遇到了ModelAwareValidationAttribute类的问题。 当我添加自定义validation属性时,它适用于服务器端模型validation,但不适用于客户端。 如果我使用框架提供的内置属性之一,客户端不显眼的validation工作,所以我知道(或者至少我认为我知道)我已经加载了正确的javascript库。 有没有人使用此框架创建自定义validation属性,它是否与客户端不显眼的validation一起使用? 如果是这样,你做了什么让它工作? 我正在使用Asp.Net MVC 3,以防万一。

如何在MVC 3中使用多个布局?

我有四种用户(客户,管理员,经理,老师)可以使用我未来的ASP网站…所以对于他们每个人我都要创建不同的界面…… 所以我的问题: 在MVC 3中使用不同的布局是否正确? 如果没有,我可以用什么来解决我的问题? 如果在MVC 3中如何使用不同的布局是正确的? 你能给我一个或多个例子吗?

创建并流式传输图像存档zip文件以供下载C#

我正在使用MVC3中心爱的DotNetZip归档库来动态生成一个Zip文件,其中包含存储在数据库中的二进制文件的.png图像。 然后,我将生成的Zip文件流式传输出来供用户下载。 (我在保存到数据库之前validation图像数据,因此您可以假设所有图像数据都有效)。 public ActionResult PictureExport() { IEnumerable userPictures = db.UserPicture.ToList(); //”db” is a DataContext and UserPicture is the model used for uploaded pictures. DateTime today = DateTime.Now; string fileName = “attachment;filename=AllUploadedPicturesAsOf:” + today.ToString() + “.zip”; this.Response.Clear(); this.Response.ContentType = “application/zip”; this.Response.AddHeader(“Content-Disposition”, fileName); using (ZipFile zipFile = new ZipFile()) { using (MemoryStream stream = new MemoryStream()) […]

异步操作完成,但结果不会发送到浏览器

我想实现网络聊天。 后端是双WCF通道。 双通道可在控制台或winforms中运行,它实际上可在Web上运行。 我至少可以发送和接收消息。 作为基础,我使用了这篇博文 ,异步操作就完成了。 当我调试结果时,我看到消息都已准备好发送到浏览器。 [AsyncTimeout(ChatServer.MaxWaitSeconds * 1020)] // timeout is a bit longer than the internal wait public void IndexAsync() { ChatSession chatSession = this.GetChatSession(); if (chatSession != null) { this.AsyncManager.OutstandingOperations.Increment(); try { chatSession.CheckForMessagesAsync(msgs => { this.AsyncManager.Parameters[“response”] = new ChatResponse { Messages = msgs }; this.AsyncManager.OutstandingOperations.Decrement(); }); } catch (Exception ex) { […]

使用自定义validation属性时获取错误消息

我正在使用像这样的CustomValidationAttribute [CustomValidation(typeof(MyValidator),”Validate”,ErrorMessage = “Foo”)] 我的validation器包含此代码 public class MyValidator { public static ValidationResult Validate(TestProperty testProperty, ValidationContext validationContext) { if (string.IsNullOrEmpty(testProperty.Name)) { return new ValidationResult(“”); <– how can I get the error message from the custom validation attribute? } return ValidationResult.Success; } } 那么如何从自定义validation属性中获取错误消息?

如何从Action Method with Array属性返回Json?

我试图从动作方法返回一些数据作为json。 我有一个员工对象,如下所示: public class Employee { public int EmployeeID {get; set;} public string FirstName {get; set;} public string LastName {get; set;} //Other irrelevant properties } 然后我有一个视图模型如下 public Class EmployeeViewModel { public Employee Supervisor{get; set;} public List Employees } 我需要返回一个看起来完全像这样的json对象: {id: 1, name: John, children: [ {id: 1, name: Joe, children: []}, {id: 1, name: Rob, […]

ModelMetaData,自定义类属性和难以形容的问题

我想做的事情似乎很简单。 在我的index.cshtml中,我想显示WizardStepAttribute值 因此,用户将在每个页面的顶部看到, Step 1: Enter User Information 我有一个名为WizardViewModel的ViewModel。 此ViewModel具有IList Steps的属性 每个“步骤”实现接口IStepViewModel,这是一个空接口。 我有一个名为Index.cshtml的视图。 此视图显示EditorFor()当前步骤。 我有一个自定义的ModelBinder,它将View绑定到一个基于WizardViewModel.CurrentStepIndex属性实现IStepViewModel的具体类的新实例 我创建了一个自定义属性WizardStepAttribute 。 我的每个步骤类都是这样定义的。 [WizardStepAttribute(Name=”Enter User Information”)] [Serializable] public class Step1 : IStepViewModel …. 我有几个问题。 对于WizardViewModel我的视图是强类型, WizardViewModel不是每一步。 我不想为IStepViewModel每个具体实现创建一个视图 我以为我可以在界面中添加属性,但是我必须在每个类中明确地实现它。 (所以这不是更好) 我想我可以使用界面中的reflection来实现它,但是,你不能在接口中的方法中引用实例。