I'm new to MVC2 and was wondering if it was possible to pass an object into a Controller Action from a View?
e.g.
I've created a ViewModel containing a list of DirectoryInfo objects and pass this to my Index view.
In the view, I loop through all of the DI objects and for each one create an ActionLink.
<%:Html.ActionLink(linkText: subfolder.Name,actionName: "Reports",
routeValues: new {folder=subfolder}, htmlAttributes:null )%>
but in my Reports action, the "folder" is always null?
public ActionResult Reports(DirectoryInfo folder)
{
//folder is always null here
Is this type of thing possible, or does the routingValue always have to be a primitive?
(ps. I have searched StackOverflow and t'internet and although I can find people asking the same question, I can't find a solution
DirectoryInfo is a complex object and I don't think you will be able to put it directly into HTML. You need to send something simple like path of directory or file from which controller can construct the required DirectoryInfo again.
Related
i have an EObject and want to get all the Properties from it. I tryed to get all Structural Features:
myEObject.eClass().getEAllStructuralFeatures()
but i get too many Properties i do not want like the object ID.
With
myEObject.eClass().getEStructuralFeatures()
there are missing some that are displayed in the Properties View.
So how can i get the same List of Properties from an EObject like the Properties View does?
Thx for your help
Just use the first option and filter out the ones you don't want (like object id) from being displayed. It's probably the easiest way.
If it must absolutely be the same as the list that is displayed in the properties view, the safest bet is to find the code used to populate the properties view and reuse it.
I am creating a plist controller class that dose things like takes my plist saves it to the document root saves all the values in my plist to variables that will be used for checking my connection responses.
also I do things like saving new values when needed.
the issue I am having however is that I want to access this one class from many classes but if I initialize it in one class and then another I am creating two objects am I not?
so my question is whats the best way for handling a class that has to be accessed from a couple of different classes?
Where are you using these other classes? If they are view controllers then creating an instance in each is perfectly fine.
If they are objects created within the same view controller, try having your objects take your object as function input. That way you can create one instance of that object and share it with all your other objects. Here is an example:
-(void)myFunction:(plistControlClass *)myPlistController{
}
I'm trying to bind a list/arraylist/hashmap/etc of custom objects to my form in JSP using Spring. Right now, the controller creates a Map of the two lists (Boolean list and custom object list) in referenceData(), and provides it to the form which uses those values to populate the fields. The values are initialized from a MySQL database using Hibernate, and all that works fine. The list is a known length before the form is initialized, so that part is easier. Now what I'd like to do is correctly bind those objects in the form, so that when there are changes made, I can detect that in onSubmit() (or wherever is appropriate), and update the database accordingly. I can't seem to bind them correctly in the form so that I can see changes made. I tried just using a list of the form fields as the model, but even that wasn't working correctly. Do I just need to inject the list in a particular way? Any ideas or examples here? Any help would be greatly appreciated.
UPDATE: At Ralph's request here is the solution I used:
In my data object class, I lazy loaded a map using MapUtils.lazyMap(), with a String key and other custom object value. The other custom object is just a class that contains List<String> and getters/setters. In the corresponding .jsp file, I just nest several loops to loop through the keys first using loop.current.key and then loop2.current.value.paramsList to loop through the values for that key. This was not really what I asked for in my original post, as I was looking for a more general solution, and the lazy loading pointed me in the right direction.
In Spring 2 you need a special List in your Command object, that is able to grow if one add the x-th element event if the list has not this size yet.
One way to do that is to use LayzList decorator from commons-collections.
#Override
protected Object formBackingObject(final HttpServletRequest request)
throws Exception {
List<PosterSelectionRow> posterSelectionRowList = LazyList.decorate(
new ArrayList<PosterSelectionRow>(),
new PosterSelectionRowListFactory());
return new PosterSelectionCommand(posterSelectionRowList);
//PosterSelectionCommand contains a list of selected poster rows
}
private static class PosterSelectionRowListFactory
implements org.apache.commons.collections.Factory {
/** Invoked if the list need a new element */
public Object create() {
return = new PosterSelectionRow();
}
}
When I remember right, there is a way without that Factory stuff, but I am not sure.
In the "Admin" area of my application, an object must be available in ViewData on every page (for display in the Master template). I have already inherited from Controller, so I cannot make a simple base class that handles it. What is a good solution of doing this when not using inheritance? An ActionFilter seems interesting but I don't want to put it on every controller in the Admin area. I'm considering the following:
Custom ControllerFactory that detects Area as well
Application_BeginRequest(), though I have no knowledge on executing controller then.
Maybe you have a better solution?
In this case I would create a separate action that executes a partial view that shows the data you need. In my opinion this is the most clean solution for this kind of problem and it's easily testable and reusable.
i have a dropdown on my masterpage. you dont need viewdata for it. i did it like this
code on masterpage:
<%= Html.DropDownList("schselectr", MVC2_NASTEST.MvcApplication.masterSchooljaarList())%>
in Global.asax.cs
public static SelectList masterSchooljaarList() {
NASDataContext _db = new NASDataContext();
List<Schooljaar> newlist = _db.Schooljaars.ToList();
return new SelectList(_db.Schooljaars.ToList(), "Sch_Schooljaar", "Sch_Schooljaar");
}
so simply, it calls the method, which returns the data i need, every time you load the page. easy, clean, effective.
I need to dynamically determine the name of the action I'm in from my controller base class in MVC2 in my OnExecuting handler.
So if the Controller is Foo and the Action is Bar, I want the string "/Foo/Bar".
This seems like it should be pretty simple, but I haven't found anything when I STFW.
I'm hoping either one of you is better at the googles than me, or just knows this off of the top of your head.
Thanks.
Your filter has access to a filterContext parameter. Take a look at its ActionDescriptor property. You can get the name of the action method, metadata about its parameters, etc. If you want just the raw URL, try filterContext.HttpContext.Request.RawUrl (or some similar property).