Tag: asp.net mvc

带模型的mvc上传文件 – 第二个参数发布文件为空

我有一个带有1个字符串属性的简单模型,我在一个简单的视图上渲染。 视图如下所示: @using (Html.BeginForm(“UploadFile”, “Home”, FormMethod.Post, new { encType=”multipart/form-data” })) { @Html.TextBoxFor(m => m.FirstName) } 控制器是这样的: [HttpPost] public ActionResult UploadFile(UploadFileModel model, HttpPostedFileBase file) { // DO Stuff return View(model); } 现在,当我提交时,模型DOES被填充但第二个参数是HttpPostedFileBase是null。 但是在执行Request.Files时 – 它似乎表明发布的请求中有一个文件。 我怎么能真正得到第二个参数绑定?

如何成功使用existingResponse =“Auto”?

所以我从我的MVC网络应用程序返回详细的400错误响应。 设置existingResponse =“PassThrough”有效,但这不是我想要的。 我不希望暴露所有失败,我只想在我有自定义响应时公开它们。 自动,默认设置,但我故意设置它。 但是,文档说必须设置“SetStatus”标志,但我不知道如何做这样的事情。 我编写了以下四种控制器方法来测试它,只有BadRequestD工作。 其他人设置状态代码和状态就好了,但是正文内容是“错误请求”。 public ActionResult BadRequestA() { Response.StatusCode = 400; return Content(“weeeeee”); } public ActionResult BadRequestB() { Response.Status = “400 U DUN MESSED UP”; return Content(“weeeeee”); } public ActionResult BadRequestC() { Response.Status = “400 U DUN MESSED UP”; Response.StatusCode = 400; return Content(“weeeeee”); } public ActionResult BadRequestD() { Response.StatusCode = […]

属性无法防止过度发布

阻止MVC 4过度发布的最佳方法是什么? 根据MS消息来源,[Bind]属性应该是通过阻止传入的表单值进入数据库来防止过度发布的最简单方法。 使用最新版本的MVC和EF,这似乎没有像预期/广告一样工作,除非我遗漏了一些重要的东西。 从Wrox Professional ASP.NET MVC 4 (Jon Galloway的第7章)开始,以下类应该防止过度发布: [Bind(Exclude=”IsAdmin”)] public class User { public int ID { get; set; } public string FirstName { get; set; } public bool IsAdmin { get; set; } } 但是所有[Bind]属性都会阻止表单提交值绑定到模型。 然后,模型具有空白/默认值,并将其写回数据库。 在这种情况下,它会确保IsAdmin = false每次使用此模型调用.SaveChanges()。 任何“真实”值都会被覆盖。 这是一个巨大的安全失败。 替代语法 – 将[Bind]放在Edit controller action参数中 – 完全相同: public ActionResult Edit([Bind(Exclude […]

mvc 5表中的SelectList,DropDownList为空值

我使用下面的代码创建一个下拉列表 调节器 ViewBag.Id= new SelectList(db.TableName.OrderBy(x => x.Name),”Id”,”Name”) 视图 @Html.DropDownList(“Id”, null, htmlAttributes: new { @class = “form-control” }) 我的问题是如何修改SelectList以添加空白项目,以便为DropDownList自动添加空白项目。 谢谢。

如何将MVC 5项目模板添加到VS 2012?

我有Visual Studio 2012 ,想要添加MVC 5模板。 如何做到这一点或仅在VS 2013中可用?

自定义授权属性在WebAPI中不起作用

public class CustomAuthorizeAttribute : AuthorizationFilterAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { return true;// if my current user is authorised } } 上面是我的CustomAuthorizeAttribute类和 [CustomAuthorize] // both [CustomAuthorize] and [CustomAuthorizeAttribute ] I tried public class ProfileController : ApiController { //My Code.. } 当我打电话的时候 http://localhost:1142/api/Profile 它没有触发CustomAuthorizeAttribute 我的FilterConfig类的内容如下所示 public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { […]

HttpContext和SignalR HubCallerContext之间的统一静态类

我有很多依赖于HttpContext.Current的代码,我注意到来自SignalR集线器的请求有HttpContext.Current == null ,所以我的代码断了,例如: HttpContext.Current.Request.IsAuthenticated 所以我想出了以下内容: public static class UnifiedHttpContext { private static HubCallerContext SignalRContext { get; set; } private static int SignalRUserId { get { return WebSecurity.GetUserId(SignalRContext.User.Identity.Name); } } private static bool IsSignalRRequest { get { return SignalRContext != null; } } public static void SetSignalRContext(HubCallerContext context) { SignalRContext = context; } public static […]

想要将图像保存到文件夹并将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; […]

如何根据先前的选择过滤下拉列表

我有两个表,即州和国家。这两个是我的视图页面中的下拉列表。 我正在使用独立查询显示每个下拉值。 在表状态中我有stateid和countryid。 我需要根据国家/地区选择过滤州的值。 我甚至有一个名为Table的主表,它由状态和国家的ID组成。以下是我以前显示的方式, enter code here //获取状态值 var query = (from i in dbContext.countries join j in dbContext.States on i.Country_id equals j.Country_id where j.State_id >= 0 select new { state = j.State_name}).ToArray//To get state values 在此处输入代码 var str = (from li in dbContext.countries where li.Country_id >= 1 select new { country = li.Country_name}).ToArray();//To […]

GET请求上ASP.NET MVC的自定义模型Binder

我已经创建了一个自定义的MVC Model Binder,可以为进入服务器的每个HttpPost调用它。 但是没有调用HttpGet请求。 我应该在GET期间调用自定义模型绑定器吗? 如果是这样,我错过了什么? 如果没有,我如何编写处理来自GET请求的QueryString自定义代码? 这是我的实施…… public class CustomModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // This only gets called for POST requests. But I need this code for GET requests. } } Global.asax中 protected void Application_Start() { ModelBinders.Binders.DefaultBinder = new CustomModelBinder(); //… } 我已经研究过这些解决方案,但它们并不能满足我的需求: 通过TempData复杂类型 使用默认绑定器构建复杂类型( ?Name=John&Surname=Doe […]