asp.net-mvc-2

Create HtmlHelper instance in Controller

大憨熊 提交于 2019-12-04 04:15:37
问题 I need to do with HtmlHelper in Controller, so how i create it in Controller (asp.net mvc 2.0)? 回答1: Is this what you want? Using HtmlHelper in a Controller EDIT Use this; System.IO.TextWriter writer = new System.IO.StringWriter(); var h = new HtmlHelper(new ViewContext(ControllerContext, new WebFormView("omg"), new ViewDataDictionary(), new TempDataDictionary(), writer), new ViewPage()); string g = h.TextBox("myname").ToString(); 回答2: You can use method like this: public static HtmlHelper

How to allow download of file, that is returned as binary data from AJAX

只愿长相守 提交于 2019-12-04 03:58:26
问题 My problem is that I send to client first PDF to download, then I need to check, if some data exists in my database, then depending on that check I need to show question that if user want to download another PDF, that I generates. My Code: //Here I just make dialog for question $('#printDWInfo').dialog({ resizable: false, modal: true, autoOpen: false }); //Here is my problem :) $('#generujWydruk').click(function (event) { event.preventDefault(); $('#printForm').submit(); // <-- sending first

Back button not requesting asp.net mvc get method

强颜欢笑 提交于 2019-12-04 03:55:34
问题 We are facing a specific problem where in on click of backbutton, the default get method is not getting triggered in asp.net mvc any specific solutions ? 回答1: If the browser has the page cached, it'll use the one from cache. Try telling the response not to cache. You can do it with an ActionFilter or globally in Global.asax. httpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); httpContext.Response.Cache.SetValidUntilExpires(false); httpContext.Response.Cache.SetRevalidation

Asp.net Mvc Display template of String, but now every simple type wants to use it!

爷,独闯天下 提交于 2019-12-04 03:41:10
I created a Display Template which when passed a string renders a disabled text box <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %> <%: Html.TextBoxFor(model => model, new { disabled = "disabled" })%> Which works great. However, for some reason MVC wants to try and stuff DateTimes and Ints through it as well, which is throwing exceptions The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'. Any ideas? You don't need to strongly type the template to a String. you can try something

Editing an object in entity framework and saving it to the database in ASP.NET MVC 2.0

女生的网名这么多〃 提交于 2019-12-04 03:38:52
So I know that EF entities track their own changes and persist them to the database when savechanges is called, but what about this scenario... I have a page that is designed to edit a blog post. It has two action methods. [HttpGet] public ViewResult EditBlogPost(int Id) { //This action method gets the blog post by the id and returns the edit blog post page. BlogPost blogPost = db.BlogPosts.Where(x => x.Id == Id).FirstOrDefault(); if (blogPost == null) { ViewData["message"] = "Blog post not found."; return View("Result"); } return View("ManageBlogPost", blogPost); } [HttpPost] public

How can I make HandleErrorAttribute work with Ajax?

狂风中的少年 提交于 2019-12-04 03:28:42
问题 In my ASP.NET MVC 2 application I use HandleErrorAttribute to display a custom error page in case of unhandled exceptions, and it works perfectly unless the exception happens in an action called by Ajax.ActionLink. In this case nothing happens. Is it possible to use HandleErrorAttribute to update the target element with the contents of an "Error.ascx" partial view? 回答1: To achieve this you could write a custom action filter: public class AjaxAwareHandleErrorAttribute : HandleErrorAttribute {

xVal and ASP.Net MVC 2 AddModelStateErrors Problem

吃可爱长大的小学妹 提交于 2019-12-04 03:24:11
I have an application that I've been using xVal successfully on for quite some time. It was recently updated to MVC 2. I use the standard DataAnnotations attributes on my domain models, which also implement a "Validate()" method that calls the DataAnnotationsValidationRunner. If there are any errors, that method throws a RulesException. In my controllers, I use the very typical catch for RulesException catch (RulesException e) { e.AddModelStateErrors(ModelState, "err"); } All typical stuff, nearly straight from the examples, and working fine until recently (I suspect the problems started at

Donut Caching ASP.NET MVC2

隐身守侯 提交于 2019-12-04 03:22:02
Is there a possibility to do Donut Caching with MVC2? Even with a lot of research I was not able to find a working solution. Donut caching does not work with ASP.NET MVC 2, see http://forums.asp.net/t/1521502.aspx : Supporting donut caching would require view engines to be able to send an array consisting of literal text + callbacks to the writer. This is a major plumbing change and is not something that will be included in v2 of the product. Not Supported != Not Possible http://blog.maartenballiauw.be/post/2008/07/01/Extending-ASPNET-MVC-OutputCache-ActionFilterAttribute-Adding-substitution

Only allow access to action if redirected from specific action

感情迁移 提交于 2019-12-04 03:15:41
问题 Is there a good way to restrict the access to an action, so you can only access it, if you were redirected from another action. For example: [HttpPost] public virtual ActionResult Create(MyViewModel vm) { if (ModelState.IsValid) { // do some work return RedirectToAction("CreateSuccess"); } else { return View(vm); } } public virtual ActionResult CreateSuccess() { // only allow execution if you were redirected from Action "Create" } 回答1: An easy way would be to store a flag in TempData in the

Why dataTokens are in Route?

我只是一个虾纸丫 提交于 2019-12-04 03:12:29
问题 context.MapRoute("authorized-credit-card", "owners/{ownerKey}/authorizedcreditcard/{action}", new { controller = "authorizedcreditcard", action = "index" }, new { ownerKey = nameFormat }, dataTokens: new { scheme = Uri.UriSchemeHttps }); In my route file I am having above kind of Route. So, could any one tell me what is the meaning of dataTokens: new { scheme = Uri.UriSchemeHttps ? And usage of above dataTokens inside the controller's action method ? 回答1: According to the documentation: You