MVC - Passing a Type as an action parameter - model-view-controller

Can I pass a parameter of Type to the Action method in MVC via a HTML.ActionLink().
e.g.
public ActionResult Get(Type containerType, string name)
{
throw new NotImplementedException("vkag");
}
<%=Html.ActionLink(flow.Source.Name, "Get", new { containerType = typeof(Worksheet), name = flow.Source.Name})%>
For some reason the type is always null. I also can't pass back a base type to allow for subtypes to be passed in it's place.
I don't know whether this is a limitation of the HTML ActionLink Helper or MVC.
I can't pass an Interface, I specify a base type parameter and use sub type as an argument in the place of a base type parameter, and I can't seem to pass a Type back. I'm pretty stuck here.
Is there another HTML Helper that will help?
Can I do anything with model binding?
Can I modify the state of my model during an ActionLink invocation before control is passed to my action method?
Kind a stuck here. Any help would be gratefully appreciated.
Thanks

The types you can pass into Routes must be passable via a URL if you think about it.
You cannot pass a Type as it would have to be serialised. Why not pass the containerType as a string and convert it back to a Type in the action?

Related

How does Spring bind form values into class variables?

If I use a form:form object in Spring, I can use the commandName in order to let Spring inject the class variable values.
However, I wonder, how does the controller catch this value?
#RequestMapping(method = RequestMethod.POST, value = "/form")
public String postForm(#ModelAttribute("item") Item item, ModelMap model)
{
return "result";
}
In the above code, the Item is injected. However, even changing the name of this variable (or removing the modelattribute), doesn't affect that this variable is injected with the form values.
Will spring just inject the values in the first model class found, from the form? How does Spring know that it has to inject the form into the Item item parameter?
At first I thought the variable in the controller (POST) should have the name of commandName of the form, but it does work with other names as well, strangely enough.
There is a dedicated section in the Spring Documentation describing the usage of #ModelAttribute on method arguments.
This process is known as Data Binding on submit and is following some conventions:
If #ModelAttribute is explicitely declared with a name on an argument (your case). In this case the submitted data of the form are copied over automatically under this name. You can check yourself that it is already there in your ModelMap model by invoking/inspecting model.get("item").
If there is no #ModelAttribute argument annotation at all, then the attribute name is assumed from the type essentially in your case type Item converts to attribute name item (camelCase notation) that is created for you holding a new Item with the form data-bind'ed fields. That is also there in the ModelMap (same check as above: model.get("item"))
Key point to realise is in all these cases DataBinding occurs before hitting your Post form RequestMapping.

Generic action result with Bind

Is it possible to have a ActionResult with a signature of:
[HttpPost]
public ActionResult SomeAction<T>([Bind(Prefix = typeof(T).Name)] T data)
{
MapAndUpdateModel<T>(data);
return Content(Boolean.TrueString);
}
I can't seem to use typeof(T).Name ?
Regards.
Attribute arguments must be types or compile-time constants. You can't call a method (the Name property getter) to supply a value to an attribute.
Unfortunately, BindAttribute is consumed by the MVC innards in long-ish hard-coded call chain without a trivial extension hook. If you want to add a similar attribute that would allow inference of the prefix, this is possible, but you would pretty much need to replace the ControllerActionInvoker merely in order to change the parameter binding behaviour.

Append to route collection

I have the following route data
object newsRoute = new
{
Area = "Admin",
Controller = "News",
Action = "Edit"
}
How can I append Title = "Hello" to the object routNews?
As in or similar
newsRoute.Append(Title = "Hello");
When you create the anonymous object you've effectively defined the properties of the anonymous class. I don't think you'll have much luck trying to redefine the type after the fact. You could create a new anonymous object with the new field and the original fields and copy the fields over, but I'm guessing you wouldn't want to do this.
You say that the object represents routedata, in that case it is probably a good idea to convert the anonymous object into a RouteValueDictionary instance using the following method
http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.anonymousobjecttohtmlattributes(v=VS.98).aspx
Once converted the object has normal dictionary semantics so you can add new key value pairs at will.
You should then be able to use the RouteValueDictionary to generate your urls

Teach ASP.NET MVC to treat key-less query values as boolean flags

If a query value in URL doesn't have a key, e.g.: http://site.com?update
and an action has bool argument named update.
I want that update argument to received value true if 'update' value is present in the URL, and false otherwise.
Example:
Action: public ActionResult MyAction(string name, bool update) {...}
URL: http://site.com/path?name=Bob&update
Expected action call: controller.MyAction("Bob", true);
if URL is http://site.com/path?name=Bob (notice no update)
then expected call is controller.MyAction("Bob", false);
It is not a big deal, I do know I can just get Request.Query and find values with key=null, but I want to have it done through the framework.
Where do I begin?
I'm using ASP.NET MVC 3
Doing this through the framework would probably require you to implement your own value provider with a corresponding value provider factory and add the factory to ValueProviderFactories. You would probably implement it similar to the existing QueryStringValueProviderFactory and QueryStringValueProvider but then add your own implementation of GetValue that includes the additional logic you wanted to return true/false based on if there is a value provided for the query string key. Here is a link on adding a value provider, and check out the QueryStringValueProvider in the framework.
http://mgolchin.net/posts/19/dive-deep-into-mvc-ivalueprovider

MVC Ajax Int32 error

Hi when I try to do an Ajax post to my controller I keep getting this message:
The parameters dictionary contains a
null entry for parameter 'id' of
non-nullable type 'System.Int32' for
method 'System.Web.Mvc.JsonResult
GetContactsByDepartment(Int32)' in
'Intranet.Controllers.MyController'.
An optional parameter must be a
reference type, a nullable type, or be
declared as an optional parameter.
The method head looks like this: public JsonResult GetContactsByDepartment(int id)
What am I missing? I have ensured that the id is being passed through my Jquery ajax call.
Try to rename id to for example DepartmentID.
Probably an issue with your registered routes.

Resources