tempdata

TempData value not persisting if used in view

删除回忆录丶 提交于 2019-12-22 05:47:52
问题 I am using TempData["hdn"] = "1"; in controller If I use this @{ var hdn = (string)TempData["hdn"]; } in View, TempData["hdn"] value in getting null in POST. If I skip this code in view it persists in POST. Why this is happening? 回答1: TempData values are cleared after they are read. if you want the value back in the controller after you have read it in the view, then you will need to include it in a hidden field and then read it out from the form values. something like: <input type="hidden"

Access TempData within custom middleware

你说的曾经没有我的故事 提交于 2019-12-12 20:24:23
问题 I have custom middleware that provides global error handling. If an exception is caught it should log the details with a reference number. I then want to redirect the user to an error page and only show the reference number. My research shows that TempData should be ideal for this but it only seems to be accessible from within a controller context. I tried adding the reference number to HttpContext.Items["ReferenceNumber"] = Guid.NewGuid(); But this value is lost through the redirect. How can

asp.net mvc - Detecting page refresh

帅比萌擦擦* 提交于 2019-12-11 03:14:27
问题 I understand that are similar question on StackOverflow about this problem, but none of it solved my issue, so i'm creating a new one. As the title says, i'd like to detect when an user refreshs a page. I have a page where i save some log information about what the user done on it (things like adding, removing or edting items). This log can only be saved when an user leaves the page, not by refreshing it. I tried the example below to detect if it's a refresh or a new request: public

Passing an object array as TempData[] to view

試著忘記壹切 提交于 2019-12-10 18:23:27
问题 I would like to return two values from a post action to the view in a RedirectToAction . TempData[] seems like the ideal option , as the data is only used to show a success message once the user has saved. I would like to show a small thumbnail of the image that the user just saved and the title of the saved item, in the success message. Currently I am passing all the data as a new MvcHtmlString TempData["SaveMsg"] = new MvcHtmlString("<img src=" + model.ImageUrl + " //> <h3//>" + model.Name

ASP.NET TempData isn't cleared even after reading it

偶尔善良 提交于 2019-12-10 14:18:19
问题 I have a controller action something similar to the below, TempData was initialized by my framework. I've noticed that TempData doesn't clear out the value once it is read as shown in the action "EmployeeUnderAge". When does TempData clears the data that has been read? public class HomeController : Controller { public ActionResult Index(int iD) { var employeeAge = (int)TempData["Age"]; RouteData.Values.Add("Age", employeeAge); return RedirectToAction("EmployeeUnderAge"); } public ActionResult

Null TempData when passing data from controller to view MVC

你离开我真会死。 提交于 2019-12-07 11:36:32
问题 I have the following class in a Controller passing data to a View: public ActionResult ControllerToView(){ ... TempData["example"] = "this is a message!"; ... return Redirect("http://myViewPageLink"); } In my View, I am trying to access the TempData dictionary with: @if(myCondition){ var test = TempData["example"]; <p>@test</p> } "myCondition" is always satisfied, but the TempData dictionary is always empty. Any ideas why? Is there any aditional code I have to write in order to make TempData

equivalent of ASP.NET MVC TempData in ASP.NET

你离开我真会死。 提交于 2019-12-05 09:59:41
In ASP.NET MVC, there's a TempData which can pass data one time from one page to another. What's the equivalent for this in ASP.NET? There is no direct equivalent (that is, data that is only passed to the next page). You can use Session and clear it out on the receiving page. You can use either Session or Viewstate to pass data between pages in ASP.NET application. 来源: https://stackoverflow.com/questions/6198037/equivalent-of-asp-net-mvc-tempdata-in-asp-net

TempData value not persisting if used in view

…衆ロ難τιáo~ 提交于 2019-12-05 08:11:20
I am using TempData["hdn"] = "1"; in controller If I use this @{ var hdn = (string)TempData["hdn"]; } in View, TempData["hdn"] value in getting null in POST. If I skip this code in view it persists in POST. Why this is happening? TempData values are cleared after they are read. if you want the value back in the controller after you have read it in the view, then you will need to include it in a hidden field and then read it out from the form values. something like: <input type="hidden" name="hdn" value="@hdn" /> Then in your controller, you can do: var hdn = Request.Form["hdn"] HTH Satpal

Copy ModelState Errors to TempData & Display them In the view

*爱你&永不变心* 提交于 2019-12-05 02:30:45
问题 Most of my action methods return PartialViews on success and RedirectToAction results on failure. For that, I would like to copy the model state errors into TempData so I could display them to the user. I've read several questions here on SO and some external links but none of them worked for me... I'm decorating the ActionMethod with ModelStateToTempData attribute from MvcContrib, then displaying it as follows in the view: (this is just a prototype) @if (TempData.Count > 0) { foreach (var

ASP.NET MVC Store TempData in Cookie

落花浮王杯 提交于 2019-12-04 06:29:55
Is there a way to let TempData store in a browser's cookie instead of Session State. I have Session State disabled on my site. Thanks. Nazaf, try this for removing your cookies: public void DeleteCookie(string name) { DateTime now = DateTime.UtcNow; string cookieKey = name; var cookie = new HttpCookie(cookieKey, null) { Expires = now.AddDays(-1) }; HttpContext.Response.Cookies.Set(cookie); } usage: DeleteCookie("__ControllerTempData"); You can use brock Allen's Cookie TempData provider. Is fully documented here and it's also available as a NuGet package . It takes into account, between other