viewbag

Alternative to ViewBag.Title in ASP.NET MVC 3

牧云@^-^@ 提交于 2019-12-20 11:17:09
问题 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

How can I show a viewbag as html?

一世执手 提交于 2019-12-20 10:33:45
问题 OK, quite new to ASP.Net MVC, so I'm sorry if this is a silly question, but how do I go about showing the values of a ViewBag as HTML. For Example, if ViewBag.SomeMessage contains the following text: <h3>Test</h3><p>Test</p><p>Test</p><p>Test</p><p>Test</p><p>TEST</p> How would I go about actually having the page render that as normal HTML? Or is there a much easier way of achieving this that I'm totally missing? Cheers! 回答1: Everyone is correct in the use of @Html.Raw() but I want to point

ViewBag vs ViewData performance difference in MVC?

痞子三分冷 提交于 2019-12-20 09:48:01
问题 I know that ViewData and ViewBag both use the same backing data and that neither are as good as using strongly typed models in most cases. However when choosing between the two is the dynamic nature of ViewBag slower than using ViewData? 回答1: Okay - my initial answer basically said 'no' - time for a bit of a u-turn. It should be 'no' in a perfect dynamic world - but upon closer inspection it would appear that there will either be no difference (accounting for JIT magic) or it might be ever-so

Access a Viewbag like an Array?

为君一笑 提交于 2019-12-19 08:52:13
问题 Imagine a view bag called ViewBag.Modes this contains the following: Simple Advanced Manual Complete How can I access the viewbag by index like you would in an array? e.g Simple is at index 0 then it would look like this ViewBag.Modes[0] I tried the above but it doesn't work so...How can I replicate this with viewbag or is there a workaround I can use? 回答1: This does the trick for me: Controller: public ActionResult Index() { var stringArray = new string[3] { "Manual", "Semi", "Auto"};

Heavy use of ViewBag

限于喜欢 提交于 2019-12-19 06:57:34
问题 I make heavy use of the ViewBag in my MVC application, is this considered bad practice? I'm unsure as to whether to spend time creating ViewModels (however I thought that was more suited to MVVM rather than MVC) or continue using the ViewBag heavily. What are the arguments for and against this? Example controller method would return it's model (generally a simple domain entity) as well as the following calls to the ViewBag: ViewBag.TotalItems = data.Count(); ViewBag.FilteredItems =

Select a default value in dropdownlistfor MVC 4

a 夏天 提交于 2019-12-19 05:32:24
问题 I'm trying to make a dropdownlistfor with a selected value but it doesn't work :/ And I search on the web but I don't find the solution :/ For the moment, I'm doing this : In C# : ViewBag.ID_VEH = new SelectList(db.VEHI, "ID_VEH", "COD_VEH", 4); // 4 is an example In my cshtml : @Html.DropDownListFor(model => model.ID_VEH, ViewBag.ID_VEH as SelectList) The dropdownlist is well complete but the default value is not selected :/ do you have an idea please ? 回答1: What I like to do is add a list

Storing an Anonymous Object in ViewBag

本秂侑毒 提交于 2019-12-18 12:50:52
问题 This is probably a silly question, but I am trying to stuff an anonymous object in ViewBag like so: ViewBag.Stuff = new { Name = "Test", Email = "user@domain.com" }; and access it from a View like so: @ViewBag.Stuff.Name I understand ViewBag is dynamic and that "Stuff" is an anonymous object... but when I look with the debugger from the View line above, I can see all the properties with the proper values. Why does the model binder have such a hard time with this? Is there a good way to

Lifetime of ViewBag elements in ASP.net MVC3

為{幸葍}努か 提交于 2019-12-18 05:36:24
问题 When will the values of the ViewBag be flushed or cleared ? 回答1: When you leave the view on subsequent request. ViewBag is created in the controller and it will live until the rendering of the view. In addition to this it is something that I would not recommend you using and replace it with view models. 来源: https://stackoverflow.com/questions/6858723/lifetime-of-viewbag-elements-in-asp-net-mvc3

How to use a ViewBag to create a dropdownlist?

爱⌒轻易说出口 提交于 2019-12-17 23:22:25
问题 Controller: public ActionResult Filter() { ViewBag.Accounts = BusinessLayer.AccountManager.Instance.getUserAccounts(HttpContext.User.Identity.Name); return View(); } View: <td>Account: </td> <td>@Html.DropDownListFor("accountid", new SelectList(ViewBag.Accounts, "AccountID", "AccountName"))</td> ViewBag.Accounts contains Account objects which have AccountID , AccountName and other properties. I would like a DropDownList called accountid (so that on Form Post I can pass the selected AccountID)

ViewBag vs Model, in MVC.NET [closed]

[亡魂溺海] 提交于 2019-12-17 19:37:53
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . This is more of a generic architectural question: I'm trying to decide if it's ok for my programmers to use "ViewBags" to pass data to views that already accept Models. My personal preference is to avoid ViewBags and build Robust Models containing all the data the view