viewbag

ViewBag RuntimeBinderException (MVC 4, ASPX)

送分小仙女□ 提交于 2019-12-05 18:32:10
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. [Controller] HomeController.cs: public class HomeController : Controller { var model = new BookReview() { Name

Viewbag check to see if item exists and write out html and value error

三世轮回 提交于 2019-12-05 11:19:20
问题 I'm using razor syntax and I want to check to see if certain ViewBag values are set before I spit out the html. If a value is set then I want to write it out. If not I want it to do nothing. @if (ViewBag.UserExists != null) { Response.Write(String.Format("<h3>{0}</h3>", ViewBag.UserExists)); } This doesn't appear to be working correctly. The code shows up on top of another h2 I have above the code above. I have two register controller methods. One is the get and the other accepts the post. If

ASP.NET MVC 3 Model Binding - ViewBag.Title clash with input of id=“Title”

一世执手 提交于 2019-12-05 09:06:23
There seems to be an issue with the ViewBag dynamic properties. Lets say I have: @{ ViewBag.Title = @Model.CourseName; } And then in a form on the page I have: @Html.TextBox("Title", null, new {style="width:400px;"}) Where Title is the name of a field in a database table. When the page first opens, text box with an id of "Title" takes the value of the ViewBag.Title dynamic property. I am a bit hazy on the exact details of Model Binding, but this does seem to be a bug, or if not, if it is something that occurs naturally as a result of the binding process, then it would be nice to be warned of

How can ViewBag data be saved after a form post?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 03:28:44
So I have a ViewBag.Something and this data is randomly generated. In my view, I set this to a label like so @Html.LabelFor(m => m.Something, (string)ViewBag.Something) . This works out well but when I submit the form there could be errors and if there are errors, I need this label to remain the same, I don't want dynamic/random data anymore so I wouldn't call the controller method that generated this ViewBag. Is there a way to retain this value without having some private variable in my controller? Some nice way that MVC/Razor does it? Option 1: Pass the value of "ViewBag.Something" to the

Creating a class like ASP.NET MVC 3 ViewBag?

烈酒焚心 提交于 2019-12-04 16:06:38
问题 I have a situation where I would like to do something simular to what was done with the ASP.NET MVC 3 ViewBag object where properties are created at runtime? Or is it at compile time? Anyway I was wondering how to go about creating an object with this behaviour? 回答1: Use an object of type dynamic . See this article for more information. 回答2: I created something like this: public class MyBag : DynamicObject { private readonly Dictionary<string, dynamic> _properties = new Dictionary<string,

Why does an MVC view need to exist in the View directory to work?

落爺英雄遲暮 提交于 2019-12-04 12:37:48
I have been writing a cms with MVC being used as the main engine for generating the pages. I am going well, but wanted the ability to create a unique razor template per site and possibly per view if I need to. My rules are that each project has to have a unique code which is linked with a url. Assets for each project site are stored in a way that the location relates to the project. So an assets associated with project C0001 would be stored in assets\C0001\ and for C0002: assets\C0002\ and so on. What I wanted to do, to keep things tidy, was to have the razor templates associated with a

Effectively avoiding ViewBag in ASP.NET MVC

强颜欢笑 提交于 2019-12-04 07:15:40
How do you deal with avoiding ViewBag due to its risk of error with being dynamic but also avoid having to populate a new ViewModel and pass it back to the view each time. For instance, I don't want to necessarily change the follow to expose common data normally stuffed in ViewBag. [HttpGet] void Index() { return View(); } to [HttpGet] void Index() { var messages = new MessageCollection(); messages.AddError("Uh oh!"); return View(messages); } Where in the pipeline would I add a property like ViewBag that is custom and strongly typed but have it exposed elegantly in the Controller and also the

Razor Layout from another assembly

别等时光非礼了梦想. 提交于 2019-12-04 03:49:03
问题 Is it possible to use a layout from another assembly? I spitted up my application in different layers and have a separated UI layer which contains the master layout for the designers to edit. Is it possible and what I what would my reference looks like? @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } 回答1: You may take a look at the following article which illustrates a technique allowing you to have Razor views embedded into a separate assembly. 回答2: At the end by the

Viewbag check to see if item exists and write out html and value error

落爺英雄遲暮 提交于 2019-12-03 22:04:06
I'm using razor syntax and I want to check to see if certain ViewBag values are set before I spit out the html. If a value is set then I want to write it out. If not I want it to do nothing. @if (ViewBag.UserExists != null) { Response.Write(String.Format("<h3>{0}</h3>", ViewBag.UserExists)); } This doesn't appear to be working correctly. The code shows up on top of another h2 I have above the code above. I have two register controller methods. One is the get and the other accepts the post. If the user exists I am setting a ViewBag item that needs to be displayed to the user. Thanks Don't use

Assign Attribute to @Html.DropdownList

怎甘沉沦 提交于 2019-12-03 20:15:45
问题 I want to assign some ID attribute name to my Dropdown list which is using data from the ViewBag as, ViewBag.Group = new SelectList(group, "GroupId", "Name"); and in the View side I have used Razor syntax for showing the Dropdown as, @Html.DropDownList("Group", "New", new { id="testID" } ) But I am getting the error on this line. Can I just assign ID of This Razor syntax? I know that the ID is generated with the Name "Playlist" But I am having 2 different dropdowns using same ViewBag. Because