viewbag

VS2019 - ViewBag does't exist in current context

一曲冷凌霜 提交于 2021-01-29 18:00:55
问题 I have installed VS 2019 for Mac and started building an application. In my view, I get the following errors: The name ViewBag does not exist in the current context. The name Layout does not exist in the current context. What could be the cause for the issue and How could we resolve this? 回答1: You might be missing the ../Views/Web.config file, because you created the project from an empty ASP.NET application instead of using an ASP.NET MVC template. Or You need to add MVC-specific Razor

MVC 5 ViewBag security

六月ゝ 毕业季﹏ 提交于 2021-01-27 14:29:05
问题 I am coding an MVC internet application, and I have a question in regards to using the ViewBag. In many of my controllers, I have SelectList objects, where the user can select an object. The object that is selected is a foreign key value for my model. My question is this: Should I use ViewBag for this? How secure is the ViewBag? Should I use values in my ViewModel instead of the ViewBag? Thanks in advance. 回答1: Use your view model. When the ViewBag was implemented (MVC 3) dynamic typing was

How should we pass a data to a view in a big ASP.NET MVC web site

霸气de小男生 提交于 2020-01-24 17:12:44
问题 First of all, I have been a php programmer for a long time and I am a mvc programmer newly. I did a few minor web sites that each have one or two controller at most. But I've started a website that will be very major web site. There will be a lot of data to pass to views. Now, normally I try to use model approach every time instead of ViewBag or ViewData approach. If the view demands more data, then I change the model class and then recompile the project. Especially, if the topic is an index

Can't pull data out of ViewBag in MVC

£可爱£侵袭症+ 提交于 2020-01-14 14:27:27
问题 I'm trying to pull data out of ViewBag and to insert it into a form hidden field via JS, before submit. It doesn't work. While debug, I find that part of the JS expression is kind of ignored, and as a result, a 'var' is evaluated to an empty string. Here's the JS part, which supposed to do the assignment: function setid() { var id = '@(ViewBag.id)'; $('#idField').val(id); }; in debug I get the below expression, which of course leads to a null 'id': function setid() { var id = ''; $('#idField'

How do I access a ViewBag.Title after it has been set by the underlying View?

℡╲_俬逩灬. 提交于 2020-01-14 12:37:25
问题 Here's the thing. I have a MVC Action, and on that action, I have applied a custom ActionFilterAttribute to get the deserialization working. Now, what I want to do, is set some header based on the ViewBag.Title that is set inside this view. I've tried wrapping the ViewResult in my own, and overriding the ExecuteResult but the ViewBag is always empty :-(. Is this even possible or does the MVC engine reset the ViewBag once the _layout is executed? Update: Let me post some code samples, to make

How do I access a ViewBag.Title after it has been set by the underlying View?

谁说胖子不能爱 提交于 2020-01-14 12:37:20
问题 Here's the thing. I have a MVC Action, and on that action, I have applied a custom ActionFilterAttribute to get the deserialization working. Now, what I want to do, is set some header based on the ViewBag.Title that is set inside this view. I've tried wrapping the ViewResult in my own, and overriding the ExecuteResult but the ViewBag is always empty :-(. Is this even possible or does the MVC engine reset the ViewBag once the _layout is executed? Update: Let me post some code samples, to make

Create ViewBag properties based on strings

馋奶兔 提交于 2020-01-12 11:16:53
问题 Is there any way to create and use dynamic properties for ViewBag, based on strings? Something like ViewBag.CreateProperty("MyProperty"); ViewBag.Property("MyProperty") = "Myvalue"; Thank you 回答1: I just found out that ViewData can be used to create such properties for ViewBag So to create property CityErrorMessage I have to use ViewData.Add("CityErrorMessage", MyErrorMessage) and then in the view I can use @ViewBag.CityErrorMessage EDIT: I created the ViewBag's properties dynamically,

Create ViewBag properties based on strings

﹥>﹥吖頭↗ 提交于 2020-01-12 11:16:34
问题 Is there any way to create and use dynamic properties for ViewBag, based on strings? Something like ViewBag.CreateProperty("MyProperty"); ViewBag.Property("MyProperty") = "Myvalue"; Thank you 回答1: I just found out that ViewData can be used to create such properties for ViewBag So to create property CityErrorMessage I have to use ViewData.Add("CityErrorMessage", MyErrorMessage) and then in the view I can use @ViewBag.CityErrorMessage EDIT: I created the ViewBag's properties dynamically,

ViewModels or ViewBag?

折月煮酒 提交于 2020-01-08 21:52:29
问题 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>

ViewModels or ViewBag?

心不动则不痛 提交于 2020-01-08 21:51:27
问题 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>