Tag: asp.net mvc 4

想要将图像保存到文件夹并将URL保存在数据库中

我是asp.net mvc的新手。 在这里我有一个问题,在控制器图像上传任何人都可以给予帮助? 我从互联网上获得的这个示例控制器,我应该更改和编码viewnya,这里我想通过“AvatarUrl”保存图像 模型> EmployeeServices public class EmployeeModel{ [ScaffoldColumn(false)] public int EmployeeID { get; set; } [Required(ErrorMessage = “Please Enter Position ID”)] public int PositionID { get; set; } [Required(ErrorMessage = “Please Enter NO PEK”)] public string NoPEK { get; set; } [Required(ErrorMessage = “Please Enter NO KTP”)] public string NoKTP { get; set; […]

具有复合ViewModel的MVC和entity frameworkHtml.DisplayNameFor

我对使用WPF / Silverlight的MVVM非常熟悉,但这是我第一次尝试使用MVC Web应用程序…这只是我的背景。 我创建了一个名为TestSitesController的控制器,它是从我的entity framework模型(生成读/写操作和视图的模板)中的“站点”模型类自动生成的。 我修改的唯一的东西是3个点,对于某些方法,有一个默认参数Guid id = null。 我摆脱了“= null”一切正常。 这是我改变的一个例子 public ActionResult Delete(Guid id = null) { //…. } 这被改为 public ActionResult Delete(Guid id) { //…. } Site模型没有什么特别的SiteId,Abbreviation和DisplayName ……试图让这个问题尽可能简单。 好的,所以我运行网站并转到htpp://…/TestSites/,一切都很完美。 我注意到我的所有视图(创建,删除,详细信息和编辑)都使用了@model MVCWeb.MyEntities.Site,我现在完全可以使用它; 但在Index.cshtml视图中,我注意到它正在使用 @model IEnumerable 这适用于生成的模板,但我想使用“复合视图模型”,也许这就是我试图混合我的MVVM知识,但如果可能的话就会坚持下去。 在我看来,复合视图模型只是一个特定于一个视图的模型,该视图由一个或多个实体模型以及其他属性(如SelectedSiteId等)组成。 所以我创建了一个非常简单的ViewModel,名为TestSitesViewModel public class TestSitesViewModel { //Eventually this will be added to a base ViewModel to […]

ASP.NET MVC4动态表单生成

我是ASP.NET MVC的新手,需要一些帮助。 我需要创建一个动态创建表单的表单。 例如,假设我有一个名为Person的模型。 该人有名字和姓氏。 在表单中,我希望允许用户输入此人的姓名。 我知道该怎么做,但我不知道怎么做是允许用户添加多个人并使其成为单个表单提交。 在页面(视图)的初始加载时,它默认显示两个空文本框(对于名字和姓氏)。 我还有一个添加按钮,允许用户在表单中添加另一行,并为第二个人显示相同的2个文本框。 现在,如果用户提交它,它将有2个文本框用于第一个名称,2个用于姓氏。 我不知道是否有办法在没有回帖的情况下做到这一点。 或者回复我唯一的选择? 编辑:忘了提我正在使用Razor引擎。 谢谢

如何启用迁移以在MVC4中更新我的数据库?

我正在使用Visual Studio 2012中的MVC4进行项目,并在表中添加了一列。 现在,当我想调试我的项目时,错误说使用迁移来更新我的数据库。 我该怎么办? 我一直在搜索,发现了一些方法,如: protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(null); } 但不知道如何以及在何处实现这个…已经尝试过app_start , global.asax等… 我发现的是,从nuget直接在控制台中启用迁移。 但我无法做到这一点。 我使用的命令: Enable-Migrations -EnableAutomaticMigrations ==>控制台说找到了多个上下文。 要启用use,请Enable-Migrations -ContextTypeName NameOfTheNamespace.Models.DefaultConnection 但我不知道什么是-ContextTypeName ,已经尝试了很多但无法理解。 My Model Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Data.Entity.Migrations; using System.ComponentModel.DataAnnotations; using System.Data.Entity.Infrastructure; namespace Vista.Models { public class TabelaIndex […]

如何修复传递到字典中的模型项的类型是错误的?

我正在尝试运行我的第一个ASP.NET MVC应用程序。 我创建了一个cotroller和视图。 数据来自数据库。 但是,当项目可以运行但是当我尝试浏览客户页面时,我会收到以下错误。 传递到字典中的模型项的类型为’System.Collections.Generic.List`1 [MvcApplication3.Models.Customer]’,但此字典需要类型为“MvcApplication3.Models.Customer”的模型项。 我在这里有点困惑,因为错误说它已经请求了模型类型。 堆栈跟踪是 堆栈跟踪: [InvalidOperationException:传递到字典中的模型项的类型为’System.Collections.Generic.List 1[MvcApplication3.Models.Customer]’, but this dictionary requires a model item of type ‘MvcApplication3.Models.Customer’.] System.Web.Mvc.ViewDataDictionary 1[MvcApplication3.Models.Customer]’, but this dictionary requires a model item of type ‘MvcApplication3.Models.Customer’.] System.Web.Mvc.ViewDataDictionary 1[MvcApplication3.Models.Customer]’, but this dictionary requires a model item of type ‘MvcApplication3.Models.Customer’.] System.Web.Mvc.ViewDataDictionary 1.SetModel(Object value)+585211 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)+371 System.Web.Mvc.ViewPage 1.SetViewData(ViewDataDictionary viewData) +48 […]

在MVC4网站上持续运行后台进程

我有一个运行的ASP.NET MVC4网站,现在我需要一些不断运行的后台任务。 但这有可能吗? MVC4网站是一种通知系统。 其他网站可以在此网站上注册Callback URL,并且该网站应在指定的时间间隔内触发该回调URL。 所以,例如每小时一次。 通常,我访问网页时,我的网站代码才会生效。 但是为了完成这项工作,我需要一种方法让我的ASP.NET网站不断在后台运行。 因此,它可以进行检查并在必要时进行HTTP调用。 重要的是,其他网站可以通过http Web请求注册自己的Callback url,而不是通过套接字。 在ASP.NET MVC Web应用程序中是否可以这样? 或者我需要一种不同的方法吗?

如何从asp.net MVC 4中的按钮单击调用控制器

请我在MVC网站上工作,我有一个搜索页面和索引页面上的另一个搜索表单。 当我从索引页面单击搜索按钮时,我想调用相同的搜索页面控制器。 下面是我的按钮在索引页面上的方式。 <button class="btn btn-info" type="button" id="addressSearch" onclick="location.href=''”> Search 列表是我的搜索搜索页面中的操作,搜索是控制器名称。 当我单击上面的按钮时,它将返回表单中的url http:// localhost:52050 / 显示不良请求。 我怀疑它来自我的路由,我不知道如何实现这一点,请任何帮助将不胜感激。 以下是我的路由方式 routes.MapRoute( name: null, url: “Page{page}”, defaults: new { Controller = “Search”, action = “List” } ); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } );

TimeZoneInfo从时区分钟偏移

从JavaScript我已经使用Date对象上的getTimezoneOffset方法向控制器传递了用户的客户端日期时间偏离UTC的分钟数。 现在我在服务器端有这个信息,我想从它创建一个TimeZoneInfo。 这怎么可能? 如果这不可能,那么如何使用分钟偏移将服务器端的UTC日期转换为客户端的时区?

根据链接MVC4更改文化

我对MVC中的文化变化有好奇心。 我试过两种方式,但显然我错了。 在我的Web.config中,我有: 这就是我试图改变线程文化的方式: @Html.ActionLink(“Eng”, “ChangeCulture”, “Home”, new { lang=”en-US”}, new { @class = “languageSelectorEnglish” }) 第一种方法 我有以下控制器: public void ChangeCulture(string lang) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(lang); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang); Response.Cookies.Remove(“Language”); var langCookie = new HttpCookie(“Language”); langCookie[“Language”] = lang; langCookie.Value = lang; langCookie.Expires = System.DateTime.Now.AddDays(21); Response.Cookies.Add(langCookie); Response.Redirect(Request.UrlReferrer.ToString()); } 第二种方法 public ActionResult ChangeCulture(string lang) { Thread.CurrentThread.CurrentCulture = […]

ASP.NET MVC 4异步控制器回调

我正在使用MVC 4中新的Async Controllerfunction,如下所述http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4 如果我的操作可能需要10-20秒才能运行,我想提供某种状态栏来通知用户进度。 Asyncfunction有什么可以帮助解决这个问题吗? 编辑:我将尝试如何尝试并看看是否有更好的方法 public async Task GizmosAsync() { return View(“Gizmos”, await GetGizmosAsync()); } private void GetGizmosAsync() { for(int i=0; i<10; i++) { lock(_locker) { _statusMessage = String.Format("{0} of 10", i); } DoSomethingLongRunning(); } } public ActionResult Status() { return Json(new { Status = _statusMessage }); } static readonly object _locker = new […]