Tag: asp.net mvc 3

MVC 3区域路由不起作用

我在我的MVC 3应用程序中创建了一个名为“Blog”的区域。 在global.asax中,我有以下代码。 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}”, // URL with parameters new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } 这是我所在地区的代码 public class BlogAreaRegistration : AreaRegistration { public override string […]

动作filter上的简单注入属性注入

我要注入的动作filter就像这样开始 public class UserAuthorisation : AuthorizeAttribute { public IWcfClientProxy FrameworkServiceProxy { get; set; } 我已经像这样设置了我的容器: container.Register<IWcfClientProxy>( ()=> new WcfClientProxy()); container.RegisterInitializer(handler => { handler.FrameworkServiceProxy = container .GetInstance<IWcfClientProxy>(); }); 当我运行它时, FrameworkServiceProxy属性为null。 我读过这篇文章: Simple Injector:在基类中注入一个属性并按照答案。 我还在本页简单注射器文档中阅读了示例。 我不是注入基类,也许这就是问题所在? ##更新## 我正在添加更多信息,因为我认为它应该是根据史蒂文斯的回答所说的。 我正在使用NuGet包用于MVC 3.这将以下内容添加到应用程序中: public static class SimpleInjectorInitializer { /// Initialize the container and register it as MVC3 Dependency Resolver. public […]

ASP.NET MVC 3 Razor:将数据从视图传递到控制器

我是.NET的全新东西。 我有一个带有HTML表单的非常基本的网页。 我希望’onsubmit’将表单数据从View发送到Controller。 我已经看过类似的post,但没有一个涉及新的Razor语法的答案。 如何处理’onsubmit’,以及如何从Controller访问数据? 谢谢!!

在ASP.NET MVC 3中添加标头

我有一个基本的ASP.NET MVC 3应用程序。 我有一个基本操作,如下所示: [AcceptVerbs(HttpVerbs.Post)] public ActionResult AddItem(string id, string name, string description, string username) { // Do stuff return Json(new { statusCode = 1 }); } 我试图通过将在Phone Gap中托管的JQuery Mobile应用程序让某人访问此操作。 我被告知我需要在我的标题中返回Access-Control-Allow-Origin: * 。 但是,我不知道如何在标题中返回它。 有人可以告诉我该怎么做吗? 非常感谢。

asp.net mvc 3 c#post变量数组

我需要将数组传递给POST方法。 但我显然缺少某些东西我的观点看起来像这样: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage” %> 我的方法声明如下所示: [HttpPost] public ActionResult LetsTest(IEnumerable rez) 当我试图提取数据时,我得到的值不能为空。 我错过了什么?

在.NET 4.0中,WebGrid + Paging + Sorting + Filtering中的filter丢失了

我已经实现了一个WebGrid。 排序,分页和过滤不能一起使用。 它们在您单独使用时起作用。 当您将三者结合使用时,过滤不起作用。 症状: 过滤结果集,然后排序。 要么 过滤结果集,然后转到下一页。 在这两种情况下,filter都会丢失。 但它确实是页面和排序。 在后面的代码中:当通过排序或分页调用action方法时,为每个filter参数显示null。 通过filter调用操作方法时,filter参数会通过。 这告诉我,当你开始排序或分页时,它没有提交表单。 public ActionResult MyPage(int? page, int? rowsPerPage, string sort, string sortdir, string orderNumber, string person, string product) 我在SO和其他地方环顾四周。 有很多例子和人们都在询问如何做一个或另一个或全部三个。 但我只看到一个与我的问题 ,所以我在这里发布。 (他也没有解决) 我的页面实现如下: @using (Ajax.BeginForm(“MyPage”, null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = “myGrid” }, new { id = “filter” })) { […]

ASP.NET MVC中htmlAttributes的匿名类和IDictionary 之间的区别?

例如,如果检查这两个扩展方法,唯一的区别是htmlAttributes的类型,因此您可以通过两种不同的方式传递htmlAttributes: public static MvcHtmlString TextBoxFor( this HtmlHelper htmlHelper, Expression<Func> expression, IDictionary htmlAttributes); public static MvcHtmlString TextBoxFor( this HtmlHelper htmlHelper, Expression<Func> expression, object htmlAttributes); 并以下列方式之一使用它们: @Html.TextBoxFor(model => model.TagLine, new { @placeholder = “We live to make art.” }) @Html.TextBoxFor(model => model.TagLine, new Dictionary { { “placeholder”, “We live to make art.” } }) 我检查了MVC源代码,我知道在后台他们使用相同的方法,但是接受匿名对象的HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)使用HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)来使匿名对象成为字典。 在我看来,使用匿名对象的视图更清晰。 你们觉得怎么样? […]

如何使用DataContext.ExecuteCommand并获取执行的存储过程返回值?

在ac #project中,我调用存储过程如下: System.Data.Linq.DataContext dataContext = MembershipContext.GetContext(connectionString); int returnValue = dataContext.ExecuteCommand(“EXEC usp_SomeProcedure {0}, {1}, {2}”, param1, param2, param3); 但是,ExecuteCommand返回受影响的行数,而不是我的存储过程返回值。 获得这个价值的最简单方法是什么? 我需要这个,因为SP在成功时返回0,如果发生错误则返回正的int值。 现在,存储过程使用RETURN输出其返回值。 但是,我可以为SELECT更改此选项,或者如果需要,我也可以使用输出参数。

如果角色包含空格,如何编写AuthorizeAttribute

我正在使用MVC3 / 4。 但这只是授权中的一般性问题。 我所拥有的一个角色是在数据库中命名为“Trip Leader”,其中包含一个空格。 我试过[Authorize(Roles=”‘Trip Leader’, Administrator”)]但它没能奏效。 有人可以帮忙吗?

MVC 3 – ObjectContext实例已被释放,不能再用于需要连接的操作

我对C#和MVC很新,我一直在创建自己的小博客网站作为测试项目。 虽然大多数事情都在发挥作用,但我在从LINQ查询中选择多个列时遇到了问题。 只有在关于SO的问题上遇到磕磕绊绊之后,才意识到我可以使用生成的实体类作为强类型模型来处理这个问题。 我需要这个,因为我一直在创建一个数据库层(这也是我之前没有用过的东西)并试图通过该层传递匿名类型不起作用。 我理解为什么会这样,所以我很满意我正朝着正确的方向前进。 然而,这种方法似乎给了我另一个问题。 我尝试过一个简单的测试,从我的Categories表中检索2列:CategoryID和Name。 我最初尝试了以下内容: using (MyEntities db = new MyEntities()) { var model = from c in db.Categories select new Category { CategoryID = c.CategoryID, Name = c.Name }; return View(model); } 这给了我在尝试迭代视图中的模型时ObjectContext被处置错误。 在阅读完这个问题后 ,我尝试将return语句移到using块之外,并在模型上调用AsEnumerable(),如下所示: IEnumerable model; using (MyEntities db = new MyEntities()) { model = from c in db.Categories select […]