asp.net-mvc-2

Howto automatically add a specific value from current route to all generated links?

前提是你 提交于 2019-11-30 15:41:40
I have site culture in URLs like this: routes.MapRoute( "Default", "{language}/{controller}/{action}/{id}", languageDefaults, languageConstraints) And it works like a charm with a little help from custom MvcHttpHandler that sets current thread's UI culture on every request based on route value. My problem is how do I automatically add the language route value from current request to all outgoing links? E.g. when page /EN/Foo/Bar is requested, I would like this <%=Html.ActionLink( "example link", MVC.Home.Index()) %> To automatically generate the same result as this: <%=Html.ActionLink(

ASP.NET MVC - Unit testing RenderPartialViewToString() with Moq framework?

馋奶兔 提交于 2019-11-30 14:14:04
I'm using this helper method to turn my PartialViewResult into string and returning it as Json - http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/ My problem is that I'm using Moq to mock the controller, and whenever I run unit test that uses this RenderPartialViewToString() method, I got the "Object reference not set to an instance of an object." error on ControllerContext. private ProgramsController GetController() { var mockHttpContext = new Mock<ControllerContext>(); mockHttpContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("test"); mockHttpContext

Custom Validation Attribute MVC2

狂风中的少年 提交于 2019-11-30 13:40:20
I have a custom validation attribute which checks to see if two properties have the same values or not (like password and retype password): [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class EqualToPropertyAttribute : ValidationAttribute { public string CompareProperty { get; set; } public EqualToPropertyAttribute(string compareProperty) { CompareProperty = compareProperty; ErrorMessage = string.Format(Messages.EqualToError, compareProperty); } public override bool IsValid(object value) { if (value == null) { return true; }

Render View programmatically into a string

冷暖自知 提交于 2019-11-30 13:25:53
问题 I would like to get the html code a view would generate in a string, modify it in my controller, then add it to my JsonResult. I found code that would do what i'm talking about from a partial. I would like to do it from an aspx View though. -- Extra explanation: Let's say I have a page Frame.aspx that /Controller/Frame will return I would like to get my hand on the response before it out so I can to wrap it with jsonp. I do not wish to edit the return result in code every time, this is why I

Handling FileContentResult when file is not found

十年热恋 提交于 2019-11-30 13:17:24
I have a controller action that downloads a file from an azure blob based on the container reference name (i.e. full path name of the file in the blob). The code looks something like this: public FileContentResult GetDocument(String pathName) { try { Byte[] buffer = BlobStorage.DownloadFile(pathName); FileContentResult result = new FileContentResult(buffer, "PDF"); String[] folders = pathName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); // get the last one as actual "file name" based on some convention result.FileDownloadName = folders[folders.Length - 1]; return result;

How can I get all element values from Request.Form without specifying exactly which one with .GetValues(“ElementIdName”)

微笑、不失礼 提交于 2019-11-30 12:53:13
问题 Currently using the below code to create a string array (elements) that contains all string values from Request.Form.GetValues("ElementIdName"), the problem is that in order for this to work all my dropdown lists in my View have to have the same element ID name which I don't want them to for obvious reasons. So I am wondering if there's any way for me to get all the string values from Request.Form without explicitly specifying the element name. Ideally I would want to get all dropdown list

MVC2 TextBoxFor value not updating after submit?

喜欢而已 提交于 2019-11-30 12:33:42
This is a really strange behavior, and I've set up some demo code to try to figure out what's going on. Basically have a a two actions and a single view. The first action sends an empty model to the view, the section action recieves the model, alters its contents and sends it back to the same view. The wierdness is, in the view, the Model seems to have the updated values in it, but when I do an Html.TextBoxFor(x => x.PropertyNameHere) it renders a textbox with the unaltered value in it. lol... I apologize in advance for the toilet humor, but it keeps the day from getting too boring. ;) Does

passing action method parameter to ActionFilterAttribute in asp.net mvc

北慕城南 提交于 2019-11-30 11:51:17
问题 I know that I can use the filterContext to get to it. However, this is not very flexible if the action method parameter is named differently. This should work: [HttpGet] [NewAuthoriseAttribute(SomeId = id)] public ActionResult Index(int id) { ... public class NewActionFilterAttribute : ActionFilterAttribute { public int SomeId { get; set; } ... but it does not (it does not even compile). Any ideas? 回答1: Building on the answer from @Pankaj and comments from @csetzkorn: You pass the name of the

GET and POST to same Controller Action in ASP.NET MVC

拜拜、爱过 提交于 2019-11-30 11:44:54
问题 I'd like to have a single action respond to both Gets as well as Posts. I tried the following [HttpGet] [HttpPost] public ActionResult SignIn() That didn't seem to work. Any suggestions ? 回答1: This is possible using the AcceptVerbs attribute. Its a bit more verbose but more flexible. [AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)] public ActionResult SignIn() { } More on msdn. 回答2: Actions respond to both GETs and POSTs by default, so you don't have to specify anything: public ActionResult SignIn

How can I override a .svc file in my routing table?

随声附和 提交于 2019-11-30 11:37:39
问题 I have this URL that was used from some JSON post back from the main website: http://site/Services/api.svc/UpdateItem We are in the process of updating the web site slowly to ASP.Net MVC 2 and don't want to break any current URL's in the system. (JavaScript refresh issues) I have deleted the /Services/api.svc and have moved the logic for this API call into the following Controller: http://site/LegacyApi/UpdateItem Unfortunately, when adding the route, I can't seem to get it to override the