I recently installed ASP.NET MVC 3 via web platform installer. I don't have the intellisense support for ViewBag in Razor view. Intellisense works fine with model in Razor view. I tried to rebuild solution, disable ReSharper... but I couldn't get it to work.
Any help would be greatly appreciated.
The ViewBag property is typed as dynamic, which means that there is no IntelliSense.
ViewBag is an alias/alternative syntax for accessing the ViewData dictionary. The two following lines of code are equivalent:
ViewBag.Message = "My message";
ViewData["Message"] = "My message";
ViewBag offers a slightly terser syntax than ViewData. Also notice that accessing ViewData using string keys also provides no IntelliSense, so you don't really lose any functionality.
One last note is that ViewBag and ViewData use the same backing storage, so that setting a property using one method makes it available using the other method:
ViewBag.Message = "My message";
string message = ViewData["Message"];
// message is now "My message"
Adding to marcind's answer of the ViewBag being dynamic:
If you want intellisense, then you're going to have to pass in a strongly typed object and then in your view, you can set:#model Namespace.YourModel which will give you intellisense when you try to do #Model.Property
Related
I have seen a lot of people say that using Viewdata or ViewBag is not a good practice for this matter(displaying messages from the controller) because of security reasons. Everyone seems to suggest ModelState
My question is what is wrong with using viewdata to display error messages? If we arent supposed to use ViewData then what should we use it for?
As #shenku answered before, ViewData and ModelState reference the exact same thing, if you look at the code for System.Web.Mvc.Controller class you will see the implementation for ModelState is:
public ModelStateDictionary ModelState
{
get
{
return this.ViewData.ModelState;
}
}
and #JimmiTh said,
although the main use of ModelState from an "end developer"'s perspective is in the controller, ViewData is used as a container for all data that's communicated between the controller and the view. Which is why it also needs to include ModelState - because, although you'd rarely use it directly in the view, ModelState is where e.g. many of the HtmlHelper methods actually get the values from by default when rendering the view from a POST action - rather than Model.
In my simple ASP.NET application with MVC I'm trying use ViewBag.
In my view:
#{
ViewBag.Title = "SomeSampleText";
}
works.
But if I want to set ViewBag.Message = "SomeText" in controller it's null and after this in view it's also null.
Anyway InteliSence like #ViewBag or #model also not works.
It was null in View. I thougth the cause is missing some references, but it was spelling mistake in controller.
It's works but still I've issue with "red" #ViewBag and #model and missing InteliSence. It's not working also with all asp TagHelpers (I've proper dependencies and they work).
I want to add I created project from empty Asp.NET core template, not from Web Application or MVC templates.
I'm using Sitefinity 8.1 in MVC mode. I have an MVC page template with an assortment of placeholders.
I've created an MVC widget designed to be used multiple times on a page. From within the controller code I would like to be able to find out the name of the placeholder the widget is sitting in so that I may make adjustments to the widget on-the-fly.
Is this possible?
Thank you.
Instead of relying on the name of a placeholder, which I think is prone to errors, why not just introduce a public property in the controller?
e.g.
public string Message {get; set;}
Then when you drop the widget in placeholder1 you can edit its properties and put whatever you want in the Message property.
Similarly, when you drop the widget in placeholder2 - you edit its properties and set something else in the Message property.
The controller will do different things depending on the value of the property - this way it is much cleaner than relying on placeholder name.
Do the adjustments need to be server side? If not just make changes based on CSS selectors. If server side I'll have to get back to you.
I am quite new to this Umbraco MVC.
I need to pass some data bound to a model to my partial view from the GET action method.
This simply is not working in a regular MVC way.
[httpget]
public ActionResult Membership()
{
SupplierMembershipInfoModel mm = new SupplierMembershipInfoModel();
mm.ProductPackage = "sssssssss";
ViewBag.status = Request.QueryString["status"];
return PartialView("MembershipPartial", mm);
}
my view:
#model Umbraco.Web.Models.SupplierMembershipInfoModel
some html.....
<td>#Model.ProductPackage</td>
I don't get data here...and the debug never hits the action. But it hits any POST action method.
I know i am doing something wrong...but just don't know what the mistake is??
Any ideas??
As #Sebastiaan points out, the best place to start is the Umbraco community site. There is documentation specific to your issue here: http://our.umbraco.org/documentation/Reference/Templating/Mvc/child-actions
In a nutshell, you want to display a child action on your page and Umbraco uses SurfaceControllers for this. A SurfaceController is simply a Controller that inherits from Umbraco.Web.Mvc.SurfaceController. This provides you Controller with access to the Umbraco context - see here (http://our.umbraco.org/documentation/Reference/Templating/Mvc/surface-controllers).
Either way, you should read the whole documentation section on templating as it will give you a lot of insight into how Umbraco MVC is managed.
I have encountered a problem while upgrading to MVC4 and I was wondering if anyone else has seen this problem and how to get around it. Whenever I put a IDictionary, Dictionary or RouteValueDictionary to the parameter list of an action, the route values for that MVC call are being passed.
To reproduce this problem create an MVC4 web project using the default Internet Application settings and add
IDictionary<string, object> myDictionary = null
to the parameter list of the Index controller and put a breakpoint inside the method. You will see that the IDictionary parameter is populated by the model binder with the route values.
How do I turn this off, or get around this?
It looks like after looking through bug reports for MVC4 the following issue is being tracked with the Dictionary<> model binder.
http://aspnetwebstack.codeplex.com/workitem/373
The workaround seems to be to remove the Dictionary from the parameters list and do the following:
[HttpPost]
public void Index(FormCollection form)
{
var values = new Dictionary<string, object>();
this.UpdateModel(values, "values");
}
Hopefully this helps someone else. Now if they would just fix it. Does anyone have a better work around?