viewbag

ViewModels or ViewBag?

泄露秘密 提交于 2020-01-08 21:51:16
问题 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>

how to create an instance of class and set properties from a Bag object like session

徘徊边缘 提交于 2020-01-05 09:27:34
问题 the class will be declared in runtime and values are stored in a Bag object like session or ViewBag . Now I want to create an instance of the class and set it's properties using Bag data . I know that I should use reflection , but I don't know if there is any method out of box that does such things or I should create one ? session["Foo"] = "foo"; session["Bar"] = "bar"; var instance = System.Activator.CreateInstance(Type.GetType(className)); instance = ? // how to set properties using session

Set a page title from a PartialView [duplicate]

跟風遠走 提交于 2020-01-03 01:59:08
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml I'm trying to set a pageTitle from a PartialView Here is my code: PartialView template: @{ ((WebViewPage)WebPageContext.Current.Page).ViewBag.Title = "Page title"; } _Layout.cshtml: <title> @ViewBag.Title</title> How to do this? 回答1: That's not something that a partial should do. It's just not its responsibility to modify the parent

ViewBag RuntimeBinderException (MVC 4, ASPX)

≡放荡痞女 提交于 2020-01-02 07:24:42
问题 I started up a Default MVC 4 project using the ASPX engine (not Razor), and everything works fine. I can use <% Model.Name %> and <% Model.Rating %> for book ratings (instead of the Restaurant Review that is up on the asp.net/mvc tutorials site). However, my code errors on the <% ViewBag.Message %> ...so for some reason, when inheriting from the "BookReviewer" Class within the BookReview model, I can view the Model information but not the Viewbag ? Am I missing something? Code below.

c# mvc model vs viewbag

情到浓时终转凉″ 提交于 2019-12-29 08:29:06
问题 Suppose you have a list of People A and a list of People B in a page. And these two are seperate classes in L2S, representing two different tables. Therefore, you cannot pass a single model as follows: ... @model PeopleA ... @foreach(var peopleA in Model.People) ... @foreach(var peopleB in //what?) Accordingly, I guess, I have three options to follow. The first one is to devide the page into partial views so that I can pass a model through RenderAction helper. Since I will use these partial

Partial Views on mvc create view that use a dropdown list to populate the partial view's view bag is this possible in mvc?

╄→гoц情女王★ 提交于 2019-12-29 02:01:18
问题 can a Partial Views on mvc create view that is using a dropdown list that sends value from the dropdown list to a function that creates a list based on the dropdown list value selection, That is then stored in a view bag for the partial view.. Can this be done in mvc and can it be done on create view of a mvc form? I can see how something this would work in the edit view because the dropdown list value has already been selected when the page loads. But on a new Create view record nothing is

how to pass viewbag data from view to another component template that I call in this view

青春壹個敷衍的年華 提交于 2019-12-25 19:36:19
问题 I have a ViewBag data from the controller that I get in the View . However, my view is made of multiple components and a component (Which is a html template and relevant JavaScript ), has a placeholder for this data where I need to display this data. How would I pass this @ViewBag.Data to the placeholder in another view (Template here). ? Or, would it be easy to consider passing the data from Controller to another view ? Please help me with a sample snippet 回答1: If I understood correctly you

Include 'ALL' option in dropdownlist bind using ViewBag

半腔热情 提交于 2019-12-25 11:53:04
问题 I have bind the dropdownlist in view by Viewbag from controller as following : ViewBag.test = from p in _userRegisterViewModel.GetEmpPrimary().ToList().Where(a => a.UserType_id != Convert.ToInt32(Session["loginUserType"].ToString())) select new { Id = p.EmpId, Name = p.First_Name.Trim() + " " + p.Last_Name.Trim() }; In view I have bind as following : @Html.DropDownListFor(model => model.EmpId, new SelectList(@ViewBag.test, "Id", "Name"), new { @class = "form-control", id="ddlEmp" }) Now i

Null view bag and partial view

青春壹個敷衍的年華 提交于 2019-12-25 09:11:28
问题 I am trying to use partial views but I am completely lost. Originally I was trying to pass dynamic data to the partial view but I ended up getting a 403 forbidden when you press the google browser F12 key. Partial View that I have reduced to one field thinking that would allow it to load data <table cellpadding="1" border="1"> <tr> <th> FIELD LBL1 </th> </tr> @foreach (MvcProgram.Models.LIST_FULL item in @ViewBag.ListFull) { <tr> <td> @item.FIELD1_DATA </td> </tr> } </table> The error which

Null view bag and partial view

房东的猫 提交于 2019-12-25 09:07:46
问题 I am trying to use partial views but I am completely lost. Originally I was trying to pass dynamic data to the partial view but I ended up getting a 403 forbidden when you press the google browser F12 key. Partial View that I have reduced to one field thinking that would allow it to load data <table cellpadding="1" border="1"> <tr> <th> FIELD LBL1 </th> </tr> @foreach (MvcProgram.Models.LIST_FULL item in @ViewBag.ListFull) { <tr> <td> @item.FIELD1_DATA </td> </tr> } </table> The error which