Tag: asp.net mvc 4

当我上传0个文件时,为什么IEnumerable count为1?

我有一个多上传表单,我想在启动上传时检查是否有任何文件。 这是我的代码。 查看: @using (Html.BeginForm(“Upload”, “Home”, FormMethod.Post, new { enctype = “multipart/form-data”})) { } 控制器: [HttpPost] public ActionResult Upload(IEnumerable files) { if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1 if(files.Any()) Console.WriteLine(files.Any()); // display true if (files.First() == null) Console.WriteLine(“first null”); // display “first null” return View(); } 当我提交空表单时,为什么我的程序会显示结果? 我可能会检查JS我的字段,但我想了解我的IEnumerable这些数据是什么。 谢谢。

如何在选择另一个@ Html.DropDownList 后填充@ Html.DropDownList

DropDownList加载了一些类别,我需要填充另一个@ Html.DropDownList,具体取决于在第一个下拉列表中选择的选项。 这是我用来填充第一个下拉列表的代码: 在控制器上: TecnicService ListCategory = new TecnicService(); IList resultCat = ListCategory.GetCategory(); List CatDropDown = new List(); foreach (Category in resultadoCat) { CatDropDown.Add(new SelectListItem { Value = a.Id.ToString(), Text = a.Name.ToString() }); } 在视图上: @model APP.Models.DataViewModel @using (Html.BeginForm(“NewPol”, “Tecnic”, null, FormMethod.Post, new { id = “pol-data-form”, @class = “form-horizontal” })) { Category @Html.DropDownList(“BasicData”, Model.Category, […]

MVC相当于Webforms“UrlAuthorizationModule.CheckUrlAccessForPrincipal”

所以我有这个控制器: namespace MyNamespace.Controllers { [Authorize(Roles=”Administrator”)] public class MyController : Controller public ActionResult Index() { … 如您所见,只有具有管理员角色的用户才能访问MyController的Action方法。 所以,从其他地方(另一个控制器,我的库类中的另一个类等) ,我如何检查Current.User.Identity.Name是否可以访问MyController? WebForms的“UrlAuthorizationModule.CheckUrlAccessForPrincipal”之类的东西。

C#MVC 4 ViewModel不接受null DateTime

学习C#和mvc4在这里遇到了一些麻烦。 问题出现在我的应用程序的filter部分。 我有一个ViewModel,它抓取数据库的“Listar_Produtos”列表,以及一些搜索选项的字段。 我打算做的是让filter接受任何字段,即使它是空值。 因为我会根据这些参数制作filter。 我有一个Viewmodel: using Foolproof; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace Ecommerce.Models.Repository { public class Produto_Repository { public class Index_Listar_Produtos { public List Index_List_Produto { get; set; } [Display(Name = “Data de Cadastro Inicial”)] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = “{0:dd/MM/yyyy}”)] public Nullable CadastroInicialData { get; […]

System.Web.Helpers未由Visual Studio 2012发布

我完全被这个困扰了。 我通过谷歌堆栈溢出找到的想法对我不起作用,我不知道为什么。 我们最近使用.NET 4.5将项目升级到Visual Studio 2012和MVC 4,现在它无法正常发布。 我们有另一个分支只是在Visual Studio 2012中发布项目而没有升级到MVC4或.NET4.5,这似乎有效,所以我猜它不是Visual Studio问题。 就像在我们的项目中设置MVC 4的方式一样。 通过直接从我们在源代码管理中创建的lib文件夹引用DLL来添加MVC 3(但在任何项目之外)。 通过NuGet添加MVC 4。 问题是System.Web.Helpers(以及其他)未出现在已发布应用程序的bin目录中。 这意味着当它被放置在测试服务器上时,它将不会运行,因为DLL丢失了。 我已经将Copy Local设置为TRUE(实际上,它已经是,但是如果关闭再打开我会转动)。 我还读到某个地方,如果文件存在于GAC中,那么这个设置是什么并不重要,它不会复制。 但是,我已经检查过,它不在GAC中。 我已经确保MVC应用程序中的引用指向NuGet包文件夹中的文件版本。 (它不是最初的,但是我手动编辑了csproj文件来执行此操作,因为删除和读取NuGet包没有帮助) 我添加了一个post-build事件来复制相关文件(虽然它们在项目的bin目录中,但不影响发布) 根据Phil Haack的博客 ,我试图放置一个_bin_deployableAssemblies文件夹,但似乎这在Visual Studio 2012中不起作用 。 我已经尝试修改csproj文件(这只是一个MSBuild文件)来复制相关文件, 根据这个SO答案 。 但是,无论什么理由都不想工作。 我已经用尽了我可以尝试的东西。 好吧,我总是可以手动复制文件,因为有些SO答案已在其他地方提出,但这样做会失败。 有任何想法吗? UPDATE 在上面的要点中添加了更多的东西,我尝试过的东西对我来说不起作用。

如何在null时使用HttpContext.Current的会话

我正在尝试使用session但是当我调用SetSession方法时,我看到HttpContext.Current为null,所以我得到MinValue(来自gettor,因为session属性为null)如何解决这个问题? 因此,每当我试图获取会话变量时,它都不起作用,确实= D它看起来我忘记了一些东西,但…… 从控制器我在用户连接时调用SessionManager: var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) { case SignInStatus.Success: { var identityUser = UserManager.FindByEmail(model.Email); if (identityUser.Matricule.IsNotNull() && !identityUser.Matricule.HasValue) { SetAlert(“Error”); } else { SessionManager.matricule = identityUser.Matricule.Value; var user = BL.PersonnelBL.GetAll(SessionManager.matricule); SessionManager.firstName = user.prenom; SessionManager.lastName = user.nom; SessionManager.chainId = user.chaine.chaine; SessionManager.secteurId = user.chaine.secteur.Id; SessionManager.serviceId = user.chaine.service.Id; SessionManager.isAuditor […]

(MVC4)将ProgressChanged事件发送到视图

我有一个MVC4应用程序 一个View使用bootstrap进度条(这是一个局部视图)和Kendo UI上传一个这样的文件: @using MyApplication.Web.Resources.Views.Contact; @Html.ValidationSummary(true) Contact Some text… @(Html.Kendo().Upload() .Name(“files”)) @Contact.ValidationButton @Contact.CancelButton 像这样的异步控制器 : public class ContactAsyncController : AsyncController { private BackgroundWorker worker = new BackgroundWorker(); public ContactAsyncController(IContactService cs) { _contactService = cs; } // // POST: /Contact/ImportContactAsync [HttpPost] public void ImportContactAsync(IEnumerable files) { AsyncManager.OutstandingOperations.Increment(); worker.WorkerReportsProgress = true; worker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); worker.DoWork += […]

MVC Miniprofiler,所有的额外时间去了哪里?

我正在使用MVC Mini Profiler来查找为什么非常基本的页面渲染速度很慢:在本地运行时,只需要700毫秒来渲染12kb的html。 它表明所有的动作都是微不足道的(除非我点击show琐事按钮,它们甚至都没有显示),最长只有2.4毫秒。 但是,对于这些所谓的微不足道的行为,“从开始的时间”数字会跳跃大约100毫秒。 什么在这个时候使用? 是否有miniprofiler选项可以帮助显示这个时间的使用位置? 编辑:额外的时间可能发生在MVC管道 – 有什么方法来描述这个? 编辑#2:尝试使用true预编译视图并没有改善性能

如何使用Razor MVC-4将JSON数据加载到SlickGrid中

我是jquery,光滑网格和razor的新手。 我已经浏览了SlickGrid示例,并且能够将静态数据加载到SlickGrid中。 现在我正在尝试将MSSQL中的JSON数据加载到我的SlickGrid中。 我在网上看到了一些例子,但我相信我错过了那些例子中没有提到的东西。 这是我的代码。 SlickGridProducts.js var grid; var columns = [ { id: “ProductID”, name: “ProductID”, field: “ProductID”, width: 50 }, { id: “ItemDesc”, name: “ItemDesc”, field: “ItemDesc”, width: 200 }, { id: “DivName”, name: “DivName”, field: “DivName”, width: 50 }, { id: “DeptDesc”, name: “DeptDesc”, field: “DeptDesc”, width: 75 }, { id: “ClassDesc”, […]

在布局页面下拉列表 – MVC

我的问题:布局页面的下拉列表。 我读过这篇文章: ASP.NET MVC Razor传递模型来布局它或多或少类似于我的问题。 在其中一篇评论中,Mattias Jakobsson写道:“但通常的解决方案是使用RenderAction在布局页面中渲染需要自己数据的部件”。 好吧,我已经用@ Html.Action()创建了布局页面,它使用db中的日期来渲染我的drop dwon列表。 一切都很完美。 但… 我有两个页面,例如:’Home’,’About’和我在布局页面的下拉列表(ddl) 如何实现当我在’家’并且我在ddl中更改选择时它刷新’主页’页面,当我在’关于’时刷新’关于’页面。 如何通过页面存储选定的ddl值? Layout.cshtml代码的一部分: . . @Html.ActionLink(@Resources.Resource.BackToIntranet, “Index”, “Home”, null, new {@class = “link link-home grid-position-left”}) @Resources.Resource.SiteTitle @Resources.Resource.LayoutHelp @Html.ActionLink(Resources.Resource.LayoutMenuItem1, “Index”, “Home”) @Html.ActionLink(Resources.Resource.LayoutMenuItem2, “Index”, “ClimaticStation”) @Html.ActionLink(Resources.Resource.LayoutMenuItem3, “Index”, “ClimaticPoint”) @Html.ActionLink(Resources.Resource.LayoutMenuItem4, “Index”, “IcewaterExchanger”) @Html.ActionLink(Resources.Resource.LayoutMenuItem5, “Index”, “Pipeline”) @Html.ActionLink(“Zestawienie”, “YearsLength”, “Pipeline”) @Html.Partial(“~/Views/Shared/Partials/LoginPartial.cshtml”) @Html.Action(“VariantsDdl”, “MyBase”) @RenderBody() . . MyBaseController.cs […]