viewdata

ASP.NET MVC ViewData and view model best practices

℡╲_俬逩灬. 提交于 2019-12-05 15:01:01
The initial situation is that I map my domain model into a presentation model. I have to display an update/create formular with textboxes and a dropdownlist. Should the viewmodel contain a list for the dropdownlist or should I pass the data for the dropdownlist by using ViewData? When should I use ViewData and when should I not use it? Shall input fields like dropdownlists have a seperate view model? I tend to try and use ViewData as little as possible since you always need to cast values, you need to do error checking for nulls or for keys that don't exist and it clutters the views in my

Need simple example of how to populate javascript array from Viewdata list

此生再无相见时 提交于 2019-12-05 03:04:24
问题 There may be a simpler way of doing this and I am all ears if there is. My situation is I have a dropdownlist on a form which I successfully populate with text and values. I also need to have additional related string values from the same table row in the db table available on the client so when the user selects from the dropdown this related data gets populated in a textbox on the form. There are only 4 records I'm dealing with so storing on the client is no big deal. I thought about passing

ASP.NET MVC Passing Raw HTML from Controller to View

回眸只為那壹抹淺笑 提交于 2019-12-04 23:14:03
问题 I have been scratching my head about this for a few days, and I am not sure if it is an issue with my environment or the code itself basing this on being to ASP.NET MVC (although I have 5 years experience in C#). I am using a recent clean install of Win7x64 and VS 2008 with all the patches. I have raw HTML stored in a database table that is selectively loaded by the controller based on a few rules which I do not have control over. Unfortunately when attempt to stuff the value into a view data

passing viewdata to asp.net mvc masterpages

一个人想着一个人 提交于 2019-12-04 07:21:28
I'm trying to pass ViewData to my asp.net mvc masterpage for an mvc usercontrol that I keep on a masterpage. For example, I created a dropdownlist of names as an mvc usercontrol and I put that in my masterpage. The problem I am running into is passing the ViewData to the masterpage. I found this article from Microsoft which has a decent solution but I was wondering if there are other "better" solutions out there. The thing I don't like about the solution in the link is that I have to change every controller to inherit from a new controller class. http://www.asp.net/learn/MVC/tutorial-13-cs

C# Centralizing repeating VIewData in MVC

Deadly 提交于 2019-12-04 05:53:27
When a user log in into my application i want to show his name throughout the whole application. I am using the asp.net MVC framework. But what i don't want is that is have to put in every controller something like: ViewData["User"] = Session["User"]; This because you may not repeat yourself. (I believe this is the DRY [Don't Repeat Yourself] principle of OO programming.) The ViewData["User"] is on my masterpage. So my question is, what is a neat way to handle my ViewData["User"] on one place? You can do this fairly easily in either a controller base-class, or an action-filter that is applied

When is it right to use ViewData instead of ViewModels?

三世轮回 提交于 2019-12-04 00:38:00
问题 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

ASP.NET MVC Passing Raw HTML from Controller to View

人盡茶涼 提交于 2019-12-03 15:06:17
I have been scratching my head about this for a few days, and I am not sure if it is an issue with my environment or the code itself basing this on being to ASP.NET MVC (although I have 5 years experience in C#). I am using a recent clean install of Win7x64 and VS 2008 with all the patches. I have raw HTML stored in a database table that is selectively loaded by the controller based on a few rules which I do not have control over. Unfortunately when attempt to stuff the value into a view data in the control like such: ViewData["HTMLData"] = DAO.HTMLDataGet(); When I see the output, it is

How do you use usercontrols in asp.net mvc that display an “island” of data?

*爱你&永不变心* 提交于 2019-12-03 01:32:39
问题 I am trying to find out how to use usercontrols in asp.net mvc. I know how to add a usercontrol to a view and how to pass data to it. What I haven't been able to figure out is how do you do this without having to retrieve and pass the data in every single controller? For example, if I have a user control that displays the most recent posts on several but not all the pages in the site, how do I write the Controllers so that I get data for that usercontrol and pass it to the user control from

How do you use usercontrols in asp.net mvc that display an “island” of data?

二次信任 提交于 2019-12-02 14:58:56
I am trying to find out how to use usercontrols in asp.net mvc. I know how to add a usercontrol to a view and how to pass data to it. What I haven't been able to figure out is how do you do this without having to retrieve and pass the data in every single controller? For example, if I have a user control that displays the most recent posts on several but not all the pages in the site, how do I write the Controllers so that I get data for that usercontrol and pass it to the user control from only one place in the web site instead of getting and passing data in each of the different controllers

Should I always use a view model or is it ok to use ViewData?

三世轮回 提交于 2019-12-02 04:26:55
when do you think is it better to use ViewData over a view model? I have the same exact partial view in a couple of main views. I'd like to control how a partial view is rendered but I'd also prefer the partial view to accept only a view model which is a collection of records, just a pure IEnumerable<> object. I'd rather avoid to send the full view model object from a main view because it also contains a lot of different properties, objects, that control paging, sorting, filtering etc. Therefore the question is if I should create another view model for the partial view or is it ok to use