Tag: asp.net mvc

无法使用数据注释在MVC 4中设置日期时间格式

我会尝试一切但不工作这个( dd / MM / yyyy )日期格式,这总是门mm / dd / yyyy [Display(Name = “Release Date”)] [DataType(DataType.DateTime)] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = “{0:dd/MM/yyyy}”)] public Nullable release_date { get; set; } 剃刀视图 @Html.EditorFor(model => model.release_date) 使用DateTime模板。 @model Nullable @if (Model.HasValue) { @Html.TextBox(“”, String.Format(“{0:dd/MM/yyyy}”, Model.Value)) } else { @Html.TextBox(“”, String.Format(“{0:dd/MM/yyyy}”, DateTime.Now)) } @{ string name = ViewData.TemplateInfo.HtmlFieldPrefix; string […]

MongoDB引用的最佳实践

我想知道使用引用进行建模的最佳实践是什么情况。 我正在使用MongoRepository库。 public class User : Entity { publis string Id { get; set; } public string Email { get; set; } public string Password { get; set; } } public class Post : Entity { public string Id { get; set; } public string Title { get; set; } public string Summary { get; […]

在ASP.NET MVC Core项目中使用View内部的C#7function

我已经找到了与此相关的其他问题,但似乎没有一个我正在寻找的东西。 我有一个在ASP.NET Core上运行的网站,其中包含VS2017中的新项目结构。 使用C#7的代码文件function编译正常。 但是,尝试在View中使用这些function会导致一系列有关语法的错误。 我尝试安装Roslyn以便在编译视图时使用它,因为我可以告诉我,Roslyn nuget包2.x及更高版本中提供了C#7function。 但现在我得到明确说明的反馈 错误CS8059:C#6中没有function’输出变量声明’。请使用语言版本7或更高版本。 在过去,我会检查web.config,但是在ASP.NET核心项目中没有web.config,而是根目录中的几乎空的,用于处理来自IIS的请求。 我如何表明我的视图应该用Roslyn编译,因为直到运行时才能完成? 至少我认为这会解决我的问题。 编辑:这个问题与此不重复,正如我在开始时提到的那样,我也在寻找现有的问题。 这是在编译时在您的应用程序中专门启用C#7function,并且仅适用于ASP.NET应用程序。 我正在使用ASP.NET Core,它没有web.config,其中定义了任何编译设置。 另外,我正在尝试为在运行时编译的视图执行此操作,并且可能在不同的系统上。 解: 对于任何感兴趣的人,您必须将Roslyn添加到您的项目中(我知道),但您还必须配置RazorViewEngineOptions以使用指示语言版本的CSharpParseOptions (默认值为6)。 我做过这个,但我做得不对。 我需要将WithLanguageVersion()的结果分配回ParseOptions以替换它们。 services.AddMvc().AddRazorOptions(options => options.ParseOptions = options.ParseOptions.WithLanguageVersion(LanguageVersion.CSharp7));

如何在Entity Framework代码优先方法中映射自我的递归关系

我想要创建的只是基本的递归类别。 如果RootCategory_Id设置为null,则类别为root,如果将其设置为某个id,则属于某个其他类别。 我在Seed()方法中添加了两个子类别的类别进行测试,但它不起作用。 (之后我检查了数据库,插入了) 分类模型 public class Category { public int ID { get; set; } public Category RootCategory { get; set; } // This one works good, it also creates “RootCategory_Id” in database on “update-database” public ICollection ChildCategories { get; set; } // This is always null, how to map it correctly? public string […]

违反类型的inheritance安全规则:’MySql.Data.Entity.MySqlEFConfiguration’

我已经创建了一个asp.net Web应用程序,它应该连接到一个mysql数据库。 我使用entity framework6和ado.net实体数据模型。 我还遵循了以下教程: https : //dev.mysql.com/doc/connectors/en/connector-net-entityframework60.html#connector-net-ef6-config 但是调用: DbConfiguration.SetConfiguration(new MySqlEFConfiguration()); 导致我的应用程序崩溃。 它可能与此特定方法无关,因为创建DBContext也会使应用程序崩溃。 是什么导致了这个错误? .NET Framework:4.6.1 MySql.Data.Entity.EF6:6.10.4 Web.config文件: 完成错误: 违反了类型的inheritance安全规则:’MySql.Data.Entity.MySqlEFConfiguration’。 派生类型必须与基本类型的安全可访问性匹配,或者不易访问。 描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 exception详细信息:System.TypeLoadException:类型违反的inheritance安全规则:’MySql.Data.Entity.MySqlEFConfiguration’。 派生类型必须与基本类型的安全可访问性匹配,或者不易访问。

如何在asp.net mvc视图中显示数据库记录

使用ASP.NET MVC和C#,如何将一些数据库记录传递给View并以表格forms显示它们? 我需要知道如何从已返回到SqlDataReader对象的数据库传输/传递一些记录行,并将该对象传递给View,这样我就可以使用foreach显示View中对象所包含的所有记录。 以下代码是我正在尝试做的事情。 但它不起作用。 控制者: public ActionResult Students() { String connectionString = “”; String sql = “SELECT * FROM students”; SqlCommand cmd = new SqlCommand(sql, connectionString); using(SqlConnection connectionString = new SqlConnection(connectionString)) { connectionString.Open(); SqlDataReader rdr = cmd.ExecuteReader(); } ViewData.Add(“students”, rdr); return View(); } 风景: Student

有没有办法从非Web应用程序处理MVC视图(aspx文件)?

我有一个后台服务运行,它向我的网站用户发送电子邮件。 我想将电子邮件模板编写为MVC视图,以保持一致(以便可以使用相同的模型发送电子邮件以显示网页)。 不幸的是,当我尝试执行LoadControl(只是修补到BuildManager.CreateInstanceFromVirtualPath)时,我得到以下内容: System.NullReferenceException at System.Web.dll!System.Web.VirtualPath.GetCacheKey() + 0x26 bytes System.Web.dll!System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath + 0x2a bytes System.Web.dll!System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal + 0x30 bytes 似乎如果我将MvcBuildViews设置为true,那么应该有一些简单的方法来使用编译的视图来构建电子邮件模板,但我无法弄清楚如何。 我从Rick Strahl找到了以下博客,可能会有这样的伎俩: http : //www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp 但是,它似乎启动了一个完整的ASP.NET服务器来处理请求。 有没有一种简单的方法来加载MVC视图并渲染它? 或者是Rick Strahl建议的加载ASP.NET运行时的唯一方法?

如何使用iTextSharp将pdf Byte Array转换为可下载文件

嘿伙计我有这个字节数组我想转换为pdf并使其可供下载。 任何人都知道如何做到这一点? 这是我的动作控制器 public ActionResult DownloadLabTestResult(string labTestResultID) { PdfReader pdfReader = new PdfReader(“Xue_Tang.pdf”); MemoryStream stream = new MemoryStream(); PdfStamper stamper = new PdfStamper(pdfReader, stream); pdfReader.Close(); stamper.Close(); stream.Flush(); stream.Close(); byte[] pdfByte = stream.ToArray(); // So i got the byte array of the original pdf at this point. Now how do i convert this // byte array […]

安装MySQL Connector .NET后ASP.Net MVC4配置错误

我正在创建一个MVC4 Web应用程序项目。当我使用空项目并只是在浏览器上运行它工作正常。但问题是当我正在尝试创建一个Internet应用程序项目而不是Empty project.Once我创建互联网应用程序项目,当试图在浏览器上运行它时,它显示以下配置错误。据我了解它是与MySQL连接器程序集引用相关的东西。以下是错误。 Server Error in ‘/’ Application. 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: Could not load file or assembly ‘MySql.Web.v20, Version=6.9.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d’ or one of […]

ASP.net MVC – 我应该使用ViewModel中的AutoMapper到Entity Framework实体吗?

我目前正在使用AutoMapper将我的Entity Framework实体映射到我的View Model: public class ProductsController : Controller { private IProductRepository productRepository; public ProductsController(IProductRepository productRepository) { this.productRepository = productRepository; } public ActionResult Details(int id) { var product = productRepository.GetProduct(id); if( product == null ) return View(“NotFound”); ProductDetailsViewModel model = Mapper.Map(product); return View(model); } } 这很好用。 我的问题是当我需要从我的View Model转到我的实体以更新数据库时。 我应该使用AutoMapper吗? 这是一个糟糕/危险的做法吗? 看起来AutoMapper很适合将复杂类型展平为简单(平面)类型,但到目前为止,我正在努力尝试从平面/简单到更复杂的类型,例如具有各种导航属性的实体。 如果使用AutoMapper执行此操作是个坏主意,那么我的代码对于Create操作会是什么样子? public ActionResult Create(CreateProductViewModel model) […]