viewdata

Populating a dropdown from ViewData

泄露秘密 提交于 2019-11-27 16:34:37
问题 I have viewdata in my controller which is populated by a list: List<employee> tempEmpList = new List<employee>(); tempEmpList = context.employees.ToList(); ViewData["tempEmpList"] = tempEmpList; and I am passing this into my view, the question is, how do I place the content of the viewdata list into a dropdown list? The display data will be .name from the list item. I know I could do a foreach on the Viewdata and create a select list, but this seems a bit long winded 回答1: You can use the

Html.HiddenFor value property not getting set

醉酒当歌 提交于 2019-11-27 02:31:47
问题 I could have used @Html.HiddenFor(x=> ViewData["crn"]) but, I get, <input id="ViewData_crn_" name="ViewData[crn]" type="hidden" value="500" /> To somehow circumvent that issue( id=ViewData_crn_ and name=ViewData[crn] ), I tried doing the following, but the "value" attribute isn't getting set. @Html.HiddenFor(x => x.CRN, new { @value="1"}) @Html.HiddenFor(x => x.CRN, new { @Value="1"}) generates <input id="CRN" name="CRN" type="hidden" value="" /> <input Value="500" id="CRN" name="CRN" type=

Pass Additional ViewData to a Strongly-Typed Partial View

戏子无情 提交于 2019-11-26 23:49:25
问题 I have a strongly-typed Partial View that takes a ProductImage and when it is rendered I would also like to provide it with some additional ViewData which I create dynamically in the containing page. How can I pass both my strongly typed object and my custom ViewData to the partial view with the RenderPartial call? var index = 0; foreach (var image in Model.Images.OrderBy(p => p.Order)) { Html.RenderPartial("ProductImageForm", image); // < Pass 'index' to partial index++; } 回答1: RenderPartial

ASP.NET MVC - How to pass an Array to the view?

不打扰是莪最后的温柔 提交于 2019-11-26 21:49:02
问题 I'm struggling myself here, to find a easy way to pass an array from the controller to the view on ASP.NET MVC framework. so in my controller I would have something like: public class HomeController : ApplicationController { public ActionResult Index() { string[] myArray = { "value01", "value02", "value03"}; ViewData["passedArray"] = myArray; return View(); } } so in my view I would have just a call to ViewData["passedArray"] and run a loop on it. But apparently the ViewData is being received

How to set ViewBag properties for all Views without using a base class for Controllers?

梦想的初衷 提交于 2019-11-26 19:22:30
In the past I've stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controller. This allowed my to use IoC on the base controller and not just reach out into global shared for such data. I'm wondering if there is an alternate way of inserting this kind of code into the MVC pipeline? Nicholas Blumhardt Un-tried by me, but you might look at registering your views and then setting the view data during the activation process. Because views are registered on-the-fly, the registration syntax doesn't help

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'xxx'

本小妞迷上赌 提交于 2019-11-26 17:46:22
There are a couple of posts about this on Stack Overflow but none with an answer that seem to fix the problem in my current situation. I have a page with a table in it, each row has a number of text fields and a dropdown. All the dropdowns need to use the same SelectList data so I have set it up as follows: Controller ViewData["Submarkets"] = new SelectList(submarketRep.AllOrdered(), "id", "name"); View <%= Html.DropDownList("submarket_0", (SelectList)ViewData["Submarkets"], "(none)") %> I have used exactly this setup in many places, but for some reason in this particular view I get the error:

How to set ViewBag properties for all Views without using a base class for Controllers?

谁说我不能喝 提交于 2019-11-26 06:56:38
问题 In the past I\'ve stuck common properties, such as the current user, onto ViewData/ViewBag in a global fashion by having all Controllers inherit from a common base controller. This allowed my to use IoC on the base controller and not just reach out into global shared for such data. I\'m wondering if there is an alternate way of inserting this kind of code into the MVC pipeline? 回答1: Un-tried by me, but you might look at registering your views and then setting the view data during the

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