viewdata

When is it right to use ViewData instead of ViewModels?

馋奶兔 提交于 2019-12-01 03:29:48
Assuming you wanted to develop your Controllers so that you use a ViewModel to contain data for the Views you render, should all data be contained within the ViewModel? What conditions would it be ok to bypass the ViewModel? The reason I ask is I'm in a position where some of the code is using ViewData and some is using the ViewModel. I want to distribute a set of guidelines in the team on when it's right to use the ViewData, and when it's just taking shortcuts. I would like opinions from other developers who have dealt with this so that I know my guidelines aren't just me being biased. Just

How do you persist querystring values in asp.net mvc?

廉价感情. 提交于 2019-11-30 17:45:36
What is a good way to persist querystring values in asp.net mvc? If I have a url: /questions?page=2&sort=newest&items=50&showcomments=1&search=abcd On paging links I want to keep those querystring values in all the links so they persist when the user clicks on the "next page" for example (in this case the page value would change, but the rest would stay the same) I can think of 2 ways to do this: Request.Querystring in the View and add the values to the links Pass each querystring value from the Controller back into the View using ViewData Is one better than the other? Are those the only

How do you persist querystring values in asp.net mvc?

拟墨画扇 提交于 2019-11-30 01:51:22
问题 What is a good way to persist querystring values in asp.net mvc? If I have a url: /questions?page=2&sort=newest&items=50&showcomments=1&search=abcd On paging links I want to keep those querystring values in all the links so they persist when the user clicks on the "next page" for example (in this case the page value would change, but the rest would stay the same) I can think of 2 ways to do this: Request.Querystring in the View and add the values to the links Pass each querystring value from

LINQ Anonymous Types + MVC Views

◇◆丶佛笑我妖孽 提交于 2019-11-29 20:08:33
问题 I've seen many questions about this, but i've never really got the answer that I need. I'm converting a fairly large web application from Web Forms to MVC and after a while I encountred a problem with passing data to the view. In the Action I execute the code: //This is just an example ViewData["QProducts"] = from p in db.Products select new{Name = p.Name, Date = p.ToShortDateString() } ViewData["QUsers"] = from u in db.Users select u; I use a foreach loop to iterate over the objects in html,

Argh! Why does System.Web.Mvc.HandleErrorInfo get passed to my views?

匆匆过客 提交于 2019-11-29 10:50:23
问题 I'm experiencing a rather frustrating problem. My MVC site runs fine for the most part, but randomly throws an error (which shows a friendly error to the user). When I check the logs, this is what I get: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo' but this dictionary requires a model item of type 'BaseViewData'. Moments later, the same user could hit refresh and the page loads fine. I'm stuck. ;( Update: added stack

Populating a dropdown from ViewData

风流意气都作罢 提交于 2019-11-29 02:17:35
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 nemesv You can use the DropDownList html helper: @Html.DropDownList("SelectedEmployee", new SelectList((IEnumerable) ViewData[

How do I pass ViewData to a HandleError View?

不羁的心 提交于 2019-11-28 23:01:05
问题 In my Site.Master file, I have 3 simple ViewData parameters (the only 3 in my entire solution). These ViewData values are critical for every single page in my application. Since these values are used in my Site.Master, I created an abstract SiteController class that overrides the OnActionExecuting method to fill these values for every Action method on every controller in my solution. [HandleError(ExceptionType=typeof(MyException), View="MyErrorView")] public abstract class SiteController :

Html.HiddenFor value property not getting set

风流意气都作罢 提交于 2019-11-28 08:57:14
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="hidden" value="" /> Am I doing anything wrong?? Thanks Have you tried using a view model instead of

Pass Additional ViewData to a Strongly-Typed Partial View

这一生的挚爱 提交于 2019-11-28 02:47:55
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++; } RenderPartial takes another parameter that is simply a ViewDataDictionary. You're almost there, just call it like this:

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

一笑奈何 提交于 2019-11-28 01:02:14
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 by the view as System.String , probably because of the declaration on the Array DataType, but