viewbag

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

旧巷老猫 提交于 2019-11-27 01:46:26
问题 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

How ViewBag in ASP.NET MVC works

妖精的绣舞 提交于 2019-11-26 21:49:13
How does the ASP.NET MVC's ViewBag work? MSDN says it is just an Object , which intrigues me, how does "Magic" properties such as ViewBag.Foo and magic strings ViewBag["Hello"] actually work? Also, how can I make one and use it in my ASP.NET WebForms app? Examples would be really appreciated! ViewBag is of type dynamic but, is internally an System.Dynamic.ExpandoObject() It is declared like this: dynamic ViewBag = new System.Dynamic.ExpandoObject(); which is why you can do : ViewBag.Foo = "Bar"; A Sample Expander Object Code: public class ExpanderObject : DynamicObject,

MVC ViewBag Best Practice

我们两清 提交于 2019-11-26 12:08:55
问题 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? 回答1: 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

Is using ViewBag in MVC bad? [closed]

六眼飞鱼酱① 提交于 2019-11-26 11:24:29
问题 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 6 months ago . 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

Updating PartialView mvc 4

♀尐吖头ヾ 提交于 2019-11-26 11:06:29
问题 Ey! How I could refresh a Partial View with data out of the Model? First time, when the page loads it\'s working properly, but not when I call it from the Action. The structure I\'ve created looks like: Anywhere in my View: @{ Html.RenderAction(\"UpdatePoints\");} My PartialView \"UpdatePoints\": <h3>Your points are @ViewBag.points </h3> At the Controller I have: public ActionResult UpdatePoints() { ViewBag.points = _Repository.Points; return PartialView(\"UpdatePoints\"); } Thanks for your

The name &#39;ViewBag&#39; does not exist in the current context

我怕爱的太早我们不能终老 提交于 2019-11-26 08:13:50
问题 I am trying to use ViewBag in my application, I have all of the recent dlls, the latest version of MVC 3, but yet I am still getting the Error: \"The name \'ViewBag\' does not exist in the current context\" I have even uninstalled and then re-installed MVC 3 and yet there is no change. Also, I do not believe that the dll\'s are showing up in the GAC. What might be my problem? Or how to add the dll\'s to the GAC? 回答1: You need to add the MVC-specific Razor configuration to your web.config. See

How ViewBag in ASP.NET MVC works

限于喜欢 提交于 2019-11-26 08:03:41
问题 How does the ASP.NET MVC\'s ViewBag work? MSDN says it is just an Object , which intrigues me, how does \"Magic\" properties such as ViewBag.Foo and magic strings ViewBag[\"Hello\"] actually work? Also, how can I make one and use it in my ASP.NET WebForms app? Examples would be really appreciated! 回答1: ViewBag is of type dynamic but, is internally an System.Dynamic.ExpandoObject() It is declared like this: dynamic ViewBag = new System.Dynamic.ExpandoObject(); which is why you can do : ViewBag

What&#39;s the difference between ViewData and ViewBag?

核能气质少年 提交于 2019-11-26 01:06:29
问题 I saw the ViewBag in MVC 3. How\'s that different than ViewData in MVC 2? 回答1: It uses the C# 4.0 dynamic feature. It achieves the same goal as viewdata and should be avoided in favor of using strongly typed view models (the same way as viewdata should be avoided). So basically it replaces magic strings : ViewData["Foo"] with magic properties : ViewBag.Foo for which you have no compile time safety. I continue to blame Microsoft for ever introducing this concept in MVC. The name of the

What&#39;s the difference between ViewData and ViewBag?

独自空忆成欢 提交于 2019-11-26 00:24:28
I saw the ViewBag in MVC 3. How's that different than ViewData in MVC 2? Darin Dimitrov It uses the C# 4.0 dynamic feature. It achieves the same goal as viewdata and should be avoided in favor of using strongly typed view models (the same way as viewdata should be avoided). So basically it replaces magic strings : ViewData["Foo"] with magic properties : ViewBag.Foo for which you have no compile time safety. I continue to blame Microsoft for ever introducing this concept in MVC. The name of the properties are case sensitive. Internally ViewBag properties are stored as name/value pairs in the