asp.net-mvc-2

Html.ActionLink with a specified HTML id?

允我心安 提交于 2019-12-03 10:29:56
问题 I'd like to give the like generated with an Html.ActionLink an HTML id so I can change the CSS depending on where I am. I have a MasterPage with a set of links and I'd like to distinguish the active "Tab" with Jquery changing the css of that active #id Right now I'm using: <%: Html.ActionLink("Some View", "Index", "controller")%> It generates: <a href="/controller">Some View</a> I'd like to generate: <a id="something" href="/controller">Some View</a> Is that possible? I've tried: <%: Html

ASP.NET MVC redirect from attribute

不打扰是莪最后的温柔 提交于 2019-12-03 10:16:58
I'm trying to execute a Redirect from a method attribute. It seems to work: public class MyAttribute: ActionFilterAttribute { [..] public override void OnActionExecuting(ActionExecutingContext filterContext) { [..] filterContext.HttpContext.Response.Redirect(urlToRedirectTo, true); [..] The only problem is that the redirect is executed after the end of the method it's attached to, while i'd like the redirect to prevent the execution of the method. Any help? Thanks You can prevent execution of the action method by assigning an ActionResult to filterContext.Result. For example: filterContext

Null reference exception when generating a url with UrlHelper.Action method

拈花ヽ惹草 提交于 2019-12-03 10:14:23
For some reason, when certain bots visit the site, generating a url with the UrlHelper.Action method raises a null exception from System.Web.HttpServerVarsCollection.Get . I've done some debugging and the call stack originiates with an attempt to get the 'HTTP_X_ORIGINAL_URL' from the HttpContextBase.Request.ServerVariables collection. If I visit the same address directly from a browser - no problem. The page is server and no error is logged. It only seems to occur when visited by a bot. Not sure if it's relevant or not, but the site was just migrated to IIS 7.5. Still using .NET 2.0 in

How to pass an enum to Html.RadioButtonFor to get a list of radio buttons in MVC 2 RC 2, C#

流过昼夜 提交于 2019-12-03 09:45:04
问题 I'm trying to render a radio button list in MVC 2 RC 2 (C#) using the following line: <%= Html.RadioButtonFor(model => Enum.GetNames(typeof(DataCarry.ProtocolEnum)), null) %> but it's just giving me the following exception at runtime: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Is this possible and if so, how, please? 回答1: You can create a template called "Enum" in /Views/Shared/EditorTemplates

How can I pass parameters to an Action using Html.Action() in ASP.NET MVC?

送分小仙女□ 提交于 2019-12-03 09:18:40
I've been using Html.Action("ActionName", "ControllerName") to invoke child actions across controllers without needing to have the view in Views\Shared. This has been working great for displaying things like session or cookie information. Instead of just accessing cookies, I would like to pass additional parameters to Html.Action("ActionName", "ControllerName") so the action can execute different code based on the the data passed to the original view. Should I be using a different method to pass parameters to a child action in a different controller? How would one accomplish this? Roberto

LazyList<T> vs System.Lazy<List<T>> in ASP.NET MVC 2?

自闭症网瘾萝莉.ら 提交于 2019-12-03 09:13:23
问题 In Rob Conery's Storefront series, Rob makes extensive use of the LazyList<..> construct to pull data from IQueryables . How does this differ from the System.Lazy<...> construct now available in .NET 4.0 (and perhaps earlier)? More depth based on DoctaJones' great answer: Would you recommend one over the other if I wanted to operate on IQueryable as a List<T> ? I'm assuming that since Lazy<T> is in the framework now, it is a safer bet for future support and maintainability? If I want to use a

FormsAuthenticationTicket.expiration v web.config value timeout

梦想的初衷 提交于 2019-12-03 09:13:04
问题 This is an MVC2 website, I am having a problem with a FormsAuthentication ticket. A user timeouts after 30 minutes cannot re-login. During testing, the DateTime.Now.AddMinutes(30) value was set to 5000 and everything was ok, but it has now changed to 30 and that is when then the problem started From cookie creation FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, user.UserID, DateTime.Now, DateTime.Now.AddMinutes(30), false, "user,user1", FormsAuthentication

ASP.NET MVC - Use Reflection to find if a Controller Exists

风流意气都作罢 提交于 2019-12-03 09:06:11
I'm having a heck of a time figuring out how to properly implement my 404 redirecting. If I use the following <HandleError()> _ Public Class BaseController : Inherits System.Web.Mvc.Controller ''# do stuff End Class Then any unhandled error on the page will load up the "Error" view which works great. http://example.com/user/999 (where 999 is an invalid User ID) will throw an error while maintaining the original URL (this is what I want) However. If someone enters http://example.com/asdfjkl into the url (where asdfjkl is an invalid controller), then IIS is throwing the generic 404 page. (this

How to render a model property of string type as checkbox in ASP.NET MVC

心不动则不痛 提交于 2019-12-03 09:05:35
I want to display a string type as checkbox on MVC view, but returns it as string type on HTTP post. The problem is that it returns false on HTTP Post. Below is my code: View: @model List<Car> foreach(var car in Model){ bool isFourWheel = false; if(bool.TryParse(car.IsFourWheel, out isFourWheel){ @Html.CheckBox("IsFourWheel", isFourWheel); //need to be rendered as checkbox, but returns string type on HTTP POST } } Model: public class Car { public string IsFourWheel { get; set; } //bad naming, but it can contain any type, include boolean } Controller: public ActionResult Index() { var cars =

asp.net mvc image path and virtual directory

主宰稳场 提交于 2019-12-03 09:01:43
I know this has to be a duplicate, but I've been wading through the hordes of information on this and I can't get it work. I'm trying to get a site working on a client's server and they have the site installed in a virtual directory. I don't have this setup, locally, so I'm flying blind here. I'm trying to build a path to an image. (it's for Facebook OpenGraph Meta data). I need the path to the image to be a fully-qualified, absolute url. I've tried so many things, but nothing seems to work. The code below outputs a relative url, but this will not work. <% var urlHelper = VirtualPathUtility