Tag: asp.net mvc

重构类似的CHTML,使用EditorFor和LabelFor呈现不同的属性

我正在构建一个包含很多关于剃刀视图的常用代码的项目。 例: @Html.LabelFor(model => model.LayoutFrontAmount, htmlAttributes: new { @class = “control-label col-xs-12 col-sm-4 col-md-3” }) @Html.EditorFor(model => model.LayoutFrontAmount, new { htmlAttributes = new { @class = “form-control” } }) @Html.ValidationMessageFor(model => model.LayoutFrontAmount, “”, new { @class = “text-danger” }) @Html.LabelFor(model => model.LayoutFrontBackAmount, htmlAttributes: new { @class = “control-label col-xs-12 col-sm-4 col-md-3” }) @Html.EditorFor(model => model.LayoutFrontBackAmount, […]

如何对依赖于c#中的身份validation的MVC控制器操作进行unit testing?

我想(在c#中)编写MVC控制器操作的unit testing,该操作可能返回一个视图或另一个视图,具体取决于请求是否经过身份validation。 如何才能做到这一点?

来自IEnumerable的DropDownList

我只想要一个简单的下拉列表,它将由IEnumerable填充。 我有 @model WebApplication4.Models.VENDOR 该模型包含一个IEnumerable的BANK对象。 我希望下拉列表由这些对象的字段填充,BANK_NAME。 我是MVC和.NET的新手。 这似乎是一件容易的事,我只是不知道该怎么做。

ASP.Net Identity如何设置目标DB?

我正在使用Asp-Team的ASP.NET Identity Sample ,我正在尝试更改IdentityDbContext的数据库…… 我用构造函数试了一下 public MyDbContext() : base(“MyConnectionString”) { } // base is IdentityDbContext 和DbContext类一样。 这很有效,直到我尝试注册用户… await IdentityStore.CreateLocalUser(user, model.Password) 返回false …没有错误,没有。 任何想法如何解决? 编辑: Filename是Models\AppModel.cs ,还有MyDbContext class 原来是 public class MyDbContext : IdentityDbContext { } 我改成了 public class MyDbContext : IdentityDbContext { public MyDbContext() : base(“MyConnectionString”) { } } 连接字符串正在工作,因为我有其他项目使用相同,他们运行正常。

在单个控制器中使用多个模型

我已经使用数据库中的EF Designer从为我的项目创建的数据库中构建我的模型。 对于单个控制器,我需要引用多个模型。 这方面的一个例子是有一个Users表,其中包含用户部门和办公室。 我需要引用两个单独的表DepartmentPermissions和OfficePermissions来确定用户能够看到的数据。 以下是自动生成模型的示例: //—————————————————————————— // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //—————————————————————————— namespace Project.Models { using System; using System.Collections.Generic; public partial […]

ASP.Net MVC中的LDAP身份validation

我希望能够通过使用他们的域UserId和密码来validation用户,但默认的ASP.Net MVC应用程序允许用户注册userId和密码然后登录。我该怎么做? 我不希望用户能够注册; 但是,他应该能够输入他的Windows域userId和密码,并由域服务器进行身份validation。 我见过的解决方案(例如Mike的博客 )不要求用户输入他/她的UserId或密码。 如何让我的ASP.Net MVC应用程序显示登录表单并针对Windows域validation用户? 如果可能,请用样品解释

我应该如何设置集成测试以将测试数据库与Entity Framework一起使用?

我正在为应用程序编写集成测试,但无法找到有关如何为我的集成套件设置测试数据库的最佳实践。 我正在使用Entity Framework代码优先的ASP.NET MVC4应用程序。 我可以确认我的测试项目中的测试默认与我的机器上的本地开发数据库通信。 这并不理想,因为我希望每次运行测试时都有一个新的数据库。 如何设置我的测试项目以便我的测试与单独的实例进行通信? 我假设可以设置SQL Server Compact Edition实例,但我不知道如何配置它。

ASP.NET MVC属性只允许用户编辑他/她自己的内容

我有一个名为Edit的控制器方法,用户可以在其中编辑他们创建的数据,如此… public ActionResult Edit(int id) { Submission submission = unit.SubmissionRepository.GetByID(id); User user = unit.UserRepository.GetByUsername(User.Identity.Name); //Make sure the submission belongs to the user if (submission.UserID != user.UserID) { throw new SecurityException(“Unauthorized access!”); } //Carry out method } 这种方法工作正常但是放入每个控制器的Edit方法都有点乱。 每个表都有一个UserID所以我想知道是否有一种更简单的方法可以通过[Authorize]属性或其他一些机制使代码更清晰自动化。

在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式)

我非常喜欢新的asp.net(asp.net 5 \ core 1.0)web应用程序的方法,其中wwwroot文件夹是根目录,只有静态文件才能提供。 可以通过路由或其他配置在asp.net 4.5项目中使用wwwroot文件夹以类似的方式运行,这样静态文件只能从中提供,而且它是静态文件的webapp的“根”吗? (我在询问这个问题时的部分动机是我在VS2015中的asp.net 5项目中托管了一个角度应用程序,但是我需要将其转移到asp.net 4.5项目中,但是希望将现有结构保留在磁盘上) 尝试使用OWIN 我尝试使用一个空的ASP.NET 4.5 Web应用程序项目和OWIN。 所以我的文件夹结构有我的角度应用程序,在项目文件夹的wwwroot文件夹中有一个主index.html文件。 项目的根目录中没有HTML文件。 我通过nuget和以下启动文件添加了OWIN: [assembly: OwinStartup(typeof(MyApp.UI.Startup))] namespace MyApp.UI { public class Startup { public void Configuration(IAppBuilder app) { string root = AppDomain.CurrentDomain.BaseDirectory; var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, “wwwroot”)); var options = new FileServerOptions { EnableDefaultFiles = true, FileSystem = physicalFileSystem }; options.StaticFileOptions.FileSystem […]

entity frameworkC#插入数据俄语编码问题

我在我的项目中使用EDM模型。 当我通过邮寄请求在数据库中插入俄语单词时,我得到了?????? 控制器: [Authorize] [HttpPost] public string DescEdit(FormCollection formValues) { var CurrentUserPhoto = User.Identity.Name; string x = Request.Form[“id”]; Int64 id = Convert.ToInt64(x); photos upPhotoDesc = photosRepository.GetPhotosById(id, CurrentUserPhoto); upPhotoDesc.description = Request.Form[“value”]; photosRepository.Save(); return Request.Form[“value”]; } 在数据库中,所有字符集都设置为utf-8 在布局页面中,内容enc类型为utf-8 数据库代码: CREATE TABLE `photos` ( `id` bigint(255) NOT NULL AUTO_INCREMENT, `done` tinyint(1) NOT NULL DEFAULT ‘0’, `imgsmall` varchar(255) NOT […]