viewbag

mvc c# html.dropdownlist and viewbag

徘徊边缘 提交于 2019-12-03 17:50:12
问题 So I have the following (pseudo code): string selectedvalud = "C"; List<SelectListItem> list= new List<SelectListItem>(); foreach(var item in mymodelinstance.Codes){ list.Add(new SelectListItem { Text = item.Name, Value = item.Id.Tostring(), Selected = item.Id.ToString() == selectedvalue ? true : false }); } ViewBag.ListOfCodes = list; on my view: <%: Html.DropDownList("Codes", (List<SelectListItem>)ViewBag.ListOfCodes , new { style = "max-width: 600px;" })%> now, before it reaches the view,

How can I use data placed into a ViewBag by a filter in my Error.cshtml view?

人走茶凉 提交于 2019-12-03 16:29:52
问题 I have an action filter that is responsible for placing some common information into the ViewBag for use by all views in the shared _Layout.cshtml file. public class ProductInfoFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // build product info // ... (code omitted) dynamic viewBag = filterContext.Controller.ViewBag; viewBag.ProductInfo = info; } } In the shared _Layout.cshtml file, I use the information that has been

How does ViewBag in ASP.NET MVC work behind the scenes?

旧巷老猫 提交于 2019-12-03 10:24:32
I am reading a book on ASP.NET MVC and I'm wondering how the following example works: Example #1 Controller public class MyController : Controller { public ActionResult Index() { ViewBag.MyProperty = 5; return View(); } } View <h1>@ViewBag.MyProperty</h1> Now I understand that ViewBag is a dynamic object, so that's how you can set the property (though I don't know much about dynamic objects, never worked with them.) But how does the view get the specific instance of the ViewBag from the controller, even though we don't pass anything directly? I thought that the ViewBag could be a public static

Creating a class like ASP.NET MVC 3 ViewBag?

久未见 提交于 2019-12-03 10:10:59
I have a situation where I would like to do something simular to what was done with the ASP.NET MVC 3 ViewBag object where properties are created at runtime? Or is it at compile time? Anyway I was wondering how to go about creating an object with this behaviour? Use an object of type dynamic . See this article for more information. I created something like this: public class MyBag : DynamicObject { private readonly Dictionary<string, dynamic> _properties = new Dictionary<string, dynamic>( StringComparer.InvariantCultureIgnoreCase ); public override bool TryGetMember( GetMemberBinder binder,

Passing list from MVC ViewBag to JavaScript

限于喜欢 提交于 2019-12-03 07:30:00
问题 I have a list of users which I pass from my controller to my view using the view bag. Now I need to be able to pass the same list to the javascript on the page. I could reconstruct the list using a foreach loop: @foreach (var item in ViewBag.userList) //Gets list of users passed from controller and adds markers to the map { var userLat = item.LastLatitude; var userLon = item.LastLongitude; var _userId = item.Id; <script>array.push({"userId":"'@_userId'","userLat":"'@userLat'","userLon":"'

MVC Passing ViewBag value from another controller

安稳与你 提交于 2019-12-03 05:15:59
问题 I've set a value into Viewbag.message in my default HomeController and successfully display it in my Shared/_Layout.cshtml . After that I added another TestController and in the TestController view, Viewbag.message seems to be null. May I know what's wrong with it. Correct me if I'm wrong,from my understanding Viewbag.Message should be available from all over the places? 回答1: ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. Basically it is a wrapper

Passing strings with Single Qoute from MVC Razor to JavaScript

你。 提交于 2019-12-03 05:07:57
This seems so simple it's embarrassing. However, the first question is when passing a value from the new ViewBag in MVC 3.0 (Razor) into a JavaScript block, is this the correct way to do it? And more importantly, where and how do you apply the proper string replacement code to prevent a single quote from becoming &#39 as in the resultant alert below? Adding this into a single script block: alert('@ViewBag.str') // "Hi, how's it going?" Results in the following alert: Razor will HTML encode everything, so to prevent the ' from being encoded to ' , you can use alert('@Html.Raw(ViewBag.str)');

How can I use data placed into a ViewBag by a filter in my Error.cshtml view?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 05:01:33
I have an action filter that is responsible for placing some common information into the ViewBag for use by all views in the shared _Layout.cshtml file. public class ProductInfoFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // build product info // ... (code omitted) dynamic viewBag = filterContext.Controller.ViewBag; viewBag.ProductInfo = info; } } In the shared _Layout.cshtml file, I use the information that has been put into the ViewBag. ... @ViewBag.ProductInfo.Name ... If an exception occurs while processing a

Pass a simple string from controller to a view MVC3

隐身守侯 提交于 2019-12-03 04:32:41
问题 I know this seems pretty basic, and it should be, but I cant find out where I am going wrong. (I hve read other articles with similar titles on SO, and other resources on the web but still cant figure it out), so any help would be appreciated. I have a controller and in it I am setting a string variable. Now I don't mind if this takes the form of a property, an ActionResult, or a straight method. I just want a simple string that I can play with in the controller, and return it to the view.

Alternative to ViewBag.Title in ASP.NET MVC 3

非 Y 不嫁゛ 提交于 2019-12-03 01:18:47
By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor): <title>@ViewBag.Title</title> The view must then contain the following to assign the title of the page, for instance: @{ ViewBag.Title = "Log On"; } Perhaps it is just my own preference but I find that using the ViewBag to hold the title a little bit wrong (I'm thinking too much magic-string flavor). So my question is: Is this the recommended best practice for people using ASP.NET MVC 3 and razor (using a dynamic property bag) or are you opting for something more strongly typed