viewbag

ViewBag/ViewData Lifecycle

人走茶凉 提交于 2019-11-28 22:26:17
I have seen many posts about when to use ViewBag/ViewData vs ViewModel but i have not been able to find an explanation of the lifecycle of the ViewBag. For example, i have two Action methods in one Controller: // POST: /MyModel/Edit/5 [HttpPost] public ActionResult Edit(MyModel _mymodel){} and // GET: /MyModel/Edit/5 public ActionResult Edit(int id){} If i put some values in the ViewBag in the GET action method, to set up some Form labels, then when they user clicks 'Submit' button and the Form is posted back to the server via HTTP POST, the ViewBag values are no longer within the POST action

How do I render HTML from the Viewbag using MVC3 Razor

自作多情 提交于 2019-11-28 20:44:51
问题 I am trying to pass a form element into an MVC3 view by using the Viewbag and simply write the HTML to the page ... In controller: ViewBag.myData = "<input type=""hidden"" name=""example"" value=""examplevalue"">"; In view (I know I could use a helper for the form): <form action="http://exampleurl/examplepage" method="post" id="example-form"> @ViewBag.myData <input type="hidden" name="anotherexample" value="anotherexamplevalue" /> </form> This renders the myData as text in the HTML i.e.:

How to use a ViewBag to create a dropdownlist?

别来无恙 提交于 2019-11-28 18:38:33
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) and the DropDownList to display the AccountName while having the AccountID as value. What am I doing

How to create own dynamic type or dynamic object in C#?

南笙酒味 提交于 2019-11-28 15:45:46
There, is for example, ViewBag property of ControllerBase class and we can dynamically get/set values and add any number of additional fields or properties to this object, which is cool .I want to use something like that, beyond MVC application and Controller class in other types of applications. When I tried to create dynamic object and set it's property like below: 1. dynamic MyDynamic = new { A="a" }; 2. MyDynamic.A = "asd"; 3. Console.WriteLine(MyDynamic.A); I've got RuntimeBinderException with message Property or indexer '<>f__AnonymousType0.A' cannot be assigned to -- it is read only in

Partial Views on mvc create view that use a dropdown list to populate the partial view's view bag is this possible in mvc?

五迷三道 提交于 2019-11-28 14:49:37
can a Partial Views on mvc create view that is using a dropdown list that sends value from the dropdown list to a function that creates a list based on the dropdown list value selection, That is then stored in a view bag for the partial view.. Can this be done in mvc and can it be done on create view of a mvc form? I can see how something this would work in the edit view because the dropdown list value has already been selected when the page loads. But on a new Create view record nothing is selected so the list function has a null value Are partial views only for forms that have data pre

ViewBag vs Model, in MVC.NET [closed]

牧云@^-^@ 提交于 2019-11-28 10:56:56
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 requires: Approach 1: MODEL A: - List of Employees - Nullable integer, indicating which item from the list is currently selected - string firstName (empty if index is null) - string lastname (empty if index is null) Approach 2: MODEL A: - List of Employees ViewBag: - ViewBag.Index (indicating which item from the list is

MVC: Iterating a Viewbag array in javascript

我只是一个虾纸丫 提交于 2019-11-28 07:35:34
The goal is to get the data from the ViewBag.Array to a Javascript array. The data is calculated in the controller so I cannot fetch it straight from the database. I need the data to draw a chart with jqplot. Code: for(i = 0; i < @ViewBag.Array.Length; i++) { jScriptArray[i] = @ViewBag.Array[i]; } The problem is "'i' does not exist in the current context" in the @ViewBag.Array[i] but has no problems in the jScriptArray[i] . Any help is appreciated. You may try the following: var array = @Html.Raw(Json.Encode(@ViewBag.Array)); for(var i = 0; i < array.length; i++) { jScriptArray[i] = array[i];

Checking to see if ViewBag has a property or not, to conditionally inject JavaScript

好久不见. 提交于 2019-11-28 07:13:28
Consider this simple controller: Porduct product = new Product(){ // Creating a product object; }; try { productManager.SaveProduct(product); return RedirectToAction("List"); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; return View("Create", product); } Now, in my Create view, I want to check ViewBag object, to see if it has Error property or not. If it has the error property, I need to inject some JavaScript into the page, to show the error message to my user. I created an extension method to check this: public static bool Has (this object obj, string propertyName) { Type type

Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml

我怕爱的太早我们不能终老 提交于 2019-11-28 06:11:27
I am using MVC 3 with the Razor view engine. I want to set some values in the ViewBag inside a Partial View and want retrieve those values in my _Layout.cshtml. For example, when you setup a default ASP.NET MVC 3 project you get a _Layout.cshtml file in the "/Views/Shared" folder. In that _Layout.cshtml the Page Title is set like this: <title>@ViewBag.PageTitle</title> Then in "/Views/Home/About.cshtml" view the contents of the ViewBag are modified: @{ ViewBag.Title = "About Us"; } This works fine. When the About view is rendered the page title is "About Us". So, now I want to render a Partial

Can't access ViewBag in a partial view in ASP.NET MVC3

折月煮酒 提交于 2019-11-28 05:12:35
I have a controller calling a view. In the view there is a PartialView called be @Html.Partial("ViewName", model). This works fine. But in the controller I wish to put something in the viewbag what would be hard to put in the viewmodel I pass to the view. The main view have no problem accessing the ViewBag , but in the PartialView it does not return anything. Is it possible to use the ViewBag in this case or should I "hack" this data into the model I pass to the view (and the model I pass to the PartialView , and the model I pass to the PartialView nested in the first PartialView )? That