viewbag

How can I show a viewbag as html?

拈花ヽ惹草 提交于 2019-12-02 23:36:42
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! Everyone is correct in the use of @Html.Raw() but I want to point out to be careful with this, as it can make your site susceptible to XSS vulnerabilities. I would combine the

Passing list from MVC ViewBag to JavaScript

拜拜、爱过 提交于 2019-12-02 21:02:12
I have a list of users which I pass from my controller to my view using the view bag. Now I need to be able to pass the same list to the javascript on the page. I could reconstruct the list using a foreach loop: @foreach (var item in ViewBag.userList) //Gets list of users passed from controller and adds markers to the map { var userLat = item.LastLatitude; var userLon = item.LastLongitude; var _userId = item.Id; <script>array.push({"userId":"'@_userId'","userLat":"'@userLat'","userLon":"'@userLon'"});</script> } However this seems like a messy approach and required a lot of reworking if a

Pass a simple string from controller to a view MVC3

你说的曾经没有我的故事 提交于 2019-12-02 17:27:06
I know this seems pretty basic, and it should be, but I cant find out where I am going wrong. (I hve read other articles with similar titles on SO, and other resources on the web but still cant figure it out), so any help would be appreciated. I have a controller and in it I am setting a string variable. Now I don't mind if this takes the form of a property, an ActionResult, or a straight method. I just want a simple string that I can play with in the controller, and return it to the view. Essentially what I am trying to do is list the files in a given folder. So my logic is like this: Find

Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3

拈花ヽ惹草 提交于 2019-12-01 17:06:36
I guess it's something very straight forward but I can't find out how to do it. In my controller I have: public ViewResult ShowForm() { ViewBag.Title = Resources.ApplicationTitle; ViewBag.LabelStatus = Resources.Status; //Logo ViewBag.Logo =@"C:\Images\Logo.png"; return View("ShowForm"); } And in my view I try this: <div id="drawForm"> <img src="@ViewBag.Logo" alt="Logo" /> </div> However when I run this I just get the "Logo" text. Use Server.MapPath to get the correct path of the image. Suppose your images folder is inside the Content folder that is normally included in an MVC project. You

Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3

流过昼夜 提交于 2019-12-01 16:44:48
问题 I guess it's something very straight forward but I can't find out how to do it. In my controller I have: public ViewResult ShowForm() { ViewBag.Title = Resources.ApplicationTitle; ViewBag.LabelStatus = Resources.Status; //Logo ViewBag.Logo =@"C:\Images\Logo.png"; return View("ShowForm"); } And in my view I try this: <div id="drawForm"> <img src="@ViewBag.Logo" alt="Logo" /> </div> However when I run this I just get the "Logo" text. 回答1: Use Server.MapPath to get the correct path of the image.

Why not to use ViewBag heavily?

谁说我不能喝 提交于 2019-12-01 11:15:06
What if i use too many ViewBag in MVC3 page? What extreme problems will be there if i continue use them heavily. In less words, what are the disadvantages of ViewBag and what is the alternate for it? what is the alternate for it? You should only use ViewModels , It's the recommended approach. Create Strongly typed views. It would make your code cleaner and easy to maintain. What extreme problems will be there? Issue with Viewbags/ViewData is compile time checking. ViewBags are just dictionaries and you won't be aware about any issues until run time and thus it makes your code error-prone. So

Why not to use ViewBag heavily?

天大地大妈咪最大 提交于 2019-12-01 08:22:48
问题 What if i use too many ViewBag in MVC3 page? What extreme problems will be there if i continue use them heavily. In less words, what are the disadvantages of ViewBag and what is the alternate for it? 回答1: what is the alternate for it? You should only use ViewModels, It's the recommended approach. Create Strongly typed views. It would make your code cleaner and easy to maintain. What extreme problems will be there? Issue with Viewbags/ViewData is compile time checking. ViewBags are just

Access a Viewbag like an Array?

ぃ、小莉子 提交于 2019-12-01 06:12:04
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? This does the trick for me: Controller: public ActionResult Index() { var stringArray = new string[3] { "Manual", "Semi", "Auto"}; ViewBag.Collection = stringArray; return View(); } View: @foreach(var str in ViewBag.Collection) { @Html.Raw(str);

Select a default value in dropdownlistfor MVC 4

ぐ巨炮叔叔 提交于 2019-12-01 03:38:34
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 ? Dylan Slabbinck What I like to do is add a list of the items to display in the dropdownlist to my model, so I don't have to pass that list

Difference between viewbag and viewstate?

不打扰是莪最后的温柔 提交于 2019-11-30 13:13:11
问题 Is there a functional difference between ViewState in Webforms and ViewBag in MVC? They seem to be the "same thing". And can be used in the same ways. I ask because MVC promotes the stateless Web and not stuffing data in the page causing bloat and slower performance. But it seems that you can do that in MVC as well. All they did, seemingly, is just give it a new name. 回答1: ViewState in Web Forms was serializing form data into a hidden, encrypted field in the form, so data could be re-bound on