viewbag

Does a child action share the same ViewBag with its “parents” action?

被刻印的时光 ゝ 提交于 2019-11-27 21:04:20
I am confused with this: I have an action ,say Parent ,and in the corresponding view file ,I have called a child action ,say Child ,both Parent and Child actions are in the same controller. and I need the Child action and the Parent action to share some data in the ViewBag.Now ,what I should do ?Here is my question: when I call the Child action in parent's view file ,I pass the viewbag to it like this: @Html.Action(ViewBag). in my child action ,I do this: public PartialViewResult Child(Object ViewBag) { //using the data in ViewBag } Is this the right way ? Does the viewbag object passed by

ViewBag/ViewData Lifecycle

落花浮王杯 提交于 2019-11-27 14:20:34
问题 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

How do I access ViewBag from JS

折月煮酒 提交于 2019-11-27 12:49:06
问题 My attempted methods. Looking at the JS via browser, the @ViewBag.CC is just blank... (missing) var c = "#" + "@ViewBag.CC"; var d = $("#" + "@ViewBag.CC").value; var e = $("#" + "@ViewBag.CC").val(); var c = "@ViewBag.CC"; var d = $("@ViewBag.CC").value; var e = $("@ViewBag.CC").val(); 回答1: if you are using razor engine template then do the following in your view write : <script> var myJsVariable = '@ViewBag.MyVariable' </script> UPDATE: A more appropriate approach is to define a set of

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

大兔子大兔子 提交于 2019-11-27 09:22:27
问题 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

MVC ViewBag Best Practice

被刻印的时光 ゝ 提交于 2019-11-27 06:49:01
For the ViewBag, I heard it was a no-no to use. I would assume have the content from the ViewBag should be incorporated into a view model? Question: Is my assumption above the best practice. (Not to use a ViewBag and second to have it in the view model) Are there situations where a ViewBag is absolutely necessary? Shyju ViewBag is a dynamic dictionary. So when using ViewBag to transfer data between action methods and views, your compiler won't be able to catch if you make a typo in your code when trying to access the ViewBag item in your view. Your view will crash at run time :( Generally it

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

拜拜、爱过 提交于 2019-11-27 05:31:30
问题 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

Is using ViewBag in MVC bad? [closed]

廉价感情. 提交于 2019-11-27 04:57:37
It seem like mvc 3 team decided to bring in a feature for dynamic data exchange between a controller and a view called the viewbag but it is A good thing against the strongly typed view we all know about? What are some of the positive and negative aspects to using the ViewBag versus using a strongly typed view? The ViewBag is the same thing as ViewData in previous ASP.NET MVC 1 and 2. It just happens to be dynamic instead of needing to use it like a dictionary with keys. I don't think this will replace strongly typed views at all and in fact you should use Viewdata/Viewbag as little as

Does a child action share the same ViewBag with its “parents” action?

时光毁灭记忆、已成空白 提交于 2019-11-27 04:29:07
问题 I am confused with this: I have an action ,say Parent ,and in the corresponding view file ,I have called a child action ,say Child ,both Parent and Child actions are in the same controller. and I need the Child action and the Parent action to share some data in the ViewBag.Now ,what I should do ?Here is my question: when I call the Child action in parent's view file ,I pass the viewbag to it like this: @Html.Action(ViewBag). in my child action ,I do this: public PartialViewResult Child(Object

ViewModels or ViewBag?

≡放荡痞女 提交于 2019-11-27 01:57:09
I'm fairly new to MVC4, EF5 and ASP.Net, and I don't seem to be able to find a good answer anywhere. Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag? Say I have a method which populates a drop down list, and I am using a viewmodel to represent the output for the view. Am I ok to use Viewbag.DropDown = PopulateDropdown(); or would it be better to incorporate this into the ViewModel, by creating a property to hold the List<SelectListItem> created by PopulateDropdown(); ? I know how handy ViewBag is, but I'm yet to see any solid reason as to not

MVC: Iterating a Viewbag array in javascript

亡梦爱人 提交于 2019-11-27 01:53:05
问题 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. 回答1: You may try the following: var array =