asp.net-mvc-2

How can I access the DisplayName data annotation value from code?

风流意气都作罢 提交于 2019-11-26 21:57:43
问题 public static string ProductHelper(this Product p) { // Need to get the DisplayName value for p.Name property } EDIT: [MetadataType(typeof(ProductMetadata))] public partial class Product { public class ProductMetadata { [DisplayName("Product name")] public object Name { get; set; } } } 回答1: Type type = typeof(Product); DisplayNameAttribute att = (DisplayNameAttribute)type.GetProperty("Name").GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault(); This assumes the attribute

MVC3 custom outputcache

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 21:41:01
问题 I'd like to use caching in my application but the data I'm returning is specific to the logged in user. I can't use any of the out of the box caching rules when I need to vary by user. Can someone point me in the right direction on creating a custom caching attribute. From the controller I can access the user from Thread.CurrentPrincipal.Identity; or a private controller member that I initialize in the controller constructor _user Thank you. 回答1: You could use the VaryByCustom. In Global.asax

ASP.NET MVC - Catch All Route And Default Route

假装没事ソ 提交于 2019-11-26 21:38:39
In trying to get my application to produce 404 errors correctly, I have implemented a catch all route at the end of my route table, as shown below: routes.MapRoute( "NotFound", _ "{*url}", _ New With {.controller = "Error", .action = "PageNotFound"} _ ) However, to get this working, I had to remove the default route: {controller}/action/{id} But now that the default has been removed, most of my action links no longer work, and the only way I have found to get them working again is to add individual routes for each controller/action. Is there a simpler way of doing this, rather than adding a

ASP.NET MVC RequireHttps

旧巷老猫 提交于 2019-11-26 21:33:30
问题 How do I use the ASP.NET MVC 2 Preview 2 Futures RequireHttps attribute? I want to prevent unsecured HTTP requests from being sent to an action method. I want to automatically redirect to HTTPS. MSDN: RequireHttpsAttribute RequireHttpsAttribute Members RequireHttpsAttribute.HandleNonHttpsRequest Method How do I use this feature? 回答1: My guess: [RequireHttps] //apply to all actions in controller public class SomeController { //... or ... [RequireHttps] //apply to this action only public

DropDownListFor does not select value if in for loop

我的梦境 提交于 2019-11-26 21:07:56
问题 In my view <%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%> in my controller public int[ ] Countries { get; set; } public List<SelectListItem> CountryList { get; set; } When the forms gets posted there is no problem, the dropdown is populated and the values the user selects are posted. But when I try to load the form with already assigned values to the Countries[ ] it does not get selected. 回答1: I'm getting the same too. When using foreach to loop around a

The file “~/Views/Position/Edit.cshtml” cannot be requested directly because it calls the “RenderSection” method

*爱你&永不变心* 提交于 2019-11-26 20:47:35
问题 I am trying to separate all the things that I could reuse in sections, so it would be easier for me to maintain. However I got this exception: The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method I created a file called sections.cshtml with the following content: @section scripts{ <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate

What does Html.HiddenFor do?

夙愿已清 提交于 2019-11-26 20:10:31
Although I have read the documentation on Html.HiddenFor, I've not grasped what is it used for... Could somebody explain its uses and give a short example? Where should those helpers go in the code? Justin Niessner It creates a hidden input on the form for the field (from your model) that you pass it. It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user. Consider the following ViewModel class: public class ViewModel { public string Value { get; set; } public int Id { get; set; } }

mvc: does the favicon.ico also look for a controller?

最后都变了- 提交于 2019-11-26 20:04:45
问题 I get an error: "The controller for path '/favicon.ico' was not found or does not implement IController" Then I thought: how does the framework know for which files it has to instantiate a controller, because the same thing is true for script, css and other files? (never thought of that, but now the favicon is complaining, I was wondering....) But back to the error, why does that occur? 回答1: Add this to you global.asax : routes.IgnoreRoute("favicon.ico"); 回答2: You can also specify the ignore

MVC2 Routing with WCF ServiceRoute: Html.ActionLink rendering incorrect links!

萝らか妹 提交于 2019-11-26 19:58:45
问题 I have a WCF service that lives side-by-side with an MVC2 web site. I'd like for my URL for the service to look like this: http://localhost/projdir/Service The MVC site is in its infancy so it still has all its boilerplate controllers etc. The following code works at first glance in global.asax: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add(new ServiceRoute("Service", new ServiceHostFactory(), typeof(MyService)));

Data Annotations for validation, at least one required field?

南笙酒味 提交于 2019-11-26 19:57:30
问题 If I have a search object with a list of fields, can I, using the System.ComponentModel.DataAnnotations namespace, set it up to validate that at least one of the fields in the search is not null or empty? i.e All the fields are optional but at least one should always be entered. 回答1: I'd create a custom validator for this - it won't give you client side validation, just server side. Note that for this to work, you'll need to be using nullable types, as value types will default to 0 or false :