Tag: asp.net mvc 4

从MVC 4 Web Api返回匿名类型失败,出现序列化错误

我刚刚开始使用MVC 4 Web API,我似乎误解了它是如何工作的。 在Web API之前,我有一个像这样的简单MVC操作方法: public JsonResult User() { return Json(new { firstName = “Joe”, lastName = “Jacobs”, email = “joe.jacobs@gmail.com” }); } 那会很好。 在新的Web API控制器中,我正在尝试做类似的事情。 public object User() { return new { firstName = “Joe”, lastName = “Jacobs”, email = “joe.jacobs@gmail.com” } } 这会因序列化错误而失败: ‘ObjectContent`1’类型无法序列化内容类型’application / xml的响应主体; 字符集= UTF-8’ 。 内部exception: 类型’ f__AnonymousType1`3 […]

如何处理ASP MVC中的重复表单字段

我有一个表格,询问用户的个人信息和他们的家庭成员。 家庭成员部分的领域正在重复。 我的问题是处理这些重复forms的最佳做法是什么? 我目前使用AJAX重复表单,但如何从这些重复字段中收集数据? 因为有人询问我如何重复forms,我这样做: AJAX Call $(document).on(‘click’, ‘.btn-add-item’, function (e) { e.preventDefault(); var $results = $(‘#results’); $.ajax({ url: ‘/AJAX/AddFamilyForm’, type: ‘post’, success: function (data) { $(data).appendTo($results); afterAJAX(); } }); }); C#代码 [HttpPost] public PartialViewResult AddFamilyForm() { if (!Request.IsAjaxRequest()) return null; return PartialView(“_FamilyForm”); }

ASP.NET MVC4重定向到登录页面

我正在使用ASP.NET MVC 4和C#创建一个Web应用程序。 我想在使用应用程序之前登录所有用户。 我正在使用ASP.NET Membership与自定义数据库。 一种方法是检查Membership.GetUser()在每个函数中是否为null。 但是,没有比在每个函数中检查用户登录状态更简单的方法吗? (也许检查web.config,global.asax等…… ??)

如何在mvc 4的web.config文件中添加程序集

我有一个“MVC 4”项目,我想将一个程序集添加到web.config文件,但我不知道我应该把它放在哪里。 我尝试任何方式,但我找不到解决方案。 每次我收到此错误: 您必须添加对程序集’System.Data.Entity,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’的引用。 这是我的web.config文件: 请帮我。 谢谢。

基于asp.net mvc中角色的自定义用户授权

我已经为我的用户创建了自定义身份validation和授权。我面临的问题是如何让mvc检查我的users表中的角色是否与我的控制器上的[Authorize(Role)]匹配,以便将httpauthorised设置为true。以下是我的习惯课程。 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)] public class CustomAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Controller.TempData[“ErrorDetails”] = “You must be logged in to access this page”; filterContext.Result = new RedirectResult(“~/User/Login”); return; } if (filterContext.HttpContext.Request.IsAuthenticated) { using (var db = new GManagerDBEntities()) { var authorizedRoles […]

ASP.NET MVC – 如何在不离开视图的情况下调用void控制器方法?

问题背景: 我正在为MVC应用程序实现一些基本的“购物车”逻辑。 目前,当我点击下面的屏幕截图中的一个链接 – 表示为“添加到购物车”时,这将调用’ProductController’中的’AddToCart’方法,如下所示: Product.cshtml代码: @Html.ActionLink(“Add To Cart”, “AddToCart”) ProductController中的’AddToCart’方法: public void AddToCart() { //Logic to add item to the cart. } 问题: 这不是问题,但是当我在ProductDetail.cshtml视图上单击ActionLink上的“添加到购物车”按钮时,页面调用ProductController上的“AddToCart”方法并在页面上显示空白视图 – 如下所示。 我希望视图保留在’ProductDetail.cshtml’上,只需调用’AddToCart’方法,我该怎么做?

如何在数据库中保存图像并将其显示在MVC 4中的视图中?

我有一个像followin这样的表: CREATE TABLE [dbo].[tblA] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [fname] NVARCHAR(50) NULL, [lname] NVARCHAR(50) NULL, [imageUrl] NVARCHAR(50) NULL ) 我想用文件上传来上传文件。 如何在数据库中保存图像并将其显示到视图中?

Asp.net MCV4框架问题

我使用VS 2012和MVC4创建了一个网站,它是我的本地主机,但是当我发布并放置我的主机它不起作用。 IIS配置相同。 但它给了我这个错误行: 这是错误; Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: The ‘targetFramework’ attribute in the element of the Web.config file is used only to target version […]

如何将连接字符串注入IDbContextFactory 的实例?

我正在使用Entity Framework 5和Code First Migrations。 我有一个从DbContext派生的DataStore类: public class DataStore : DbContext, IDataStore { public int UserID { get; private set; } public DataStore(int userId, string connectionString) : base(connectionString) { UserID = userId; } public virtual IDbSet Users { get; set; } // Rest of code here } 还有一个工厂类,它创建DataStore类的DataStore : public class DataStoreFactory : Disposable, IDataStoreFactory […]

Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012

出于某种原因,在VS2012中, Request.CreateResponse现在是“红色”,当我将鼠标hover在IDE的使用状态时 无法解析符号’CreateResponse’ 这是ApiController类: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Filters; using GOCApi.Attributes; using GOCApi.Models; using GOCApi.Models.Abstract; using AttributeRouting; using AttributeRouting.Web.Http; namespace GOCApi.Controllers { [RoutePrefix(“Courses”)] public class CoursesController : ApiController { private ICoursesRepository _coursesRepository { get; set; } public CoursesController(ICoursesRepository coursesRepository) { _coursesRepository = coursesRepository; } [GET(“{id}”)] public […]