public ActionResult RenderMyThing(IList<String> strings)
{
return View("RenderMyView");
}
How do I pass in strings?
routes.MapRoute("MyRoute", "RenderMyThing.aspx", new { controller = "My", action = "RenderMyThing" });
Is there a way I could pass in strings here?
Secondly, how does ASP.NET MVC know that action is my action, and controller is my controller. Like I saw this in samples, and it does work, but isn't it just an anonymous object with no type?
This is the provenance of model binding: the framework needs to have some instruction as to how to turn a "request", which comes out of the routing context, query string, forms collection, etc., into the parameters that your action method wants.
The DefaultModelBinder will generate a list if it sees that you have multiple key-value pairs with the same key (and appropriately typed/convertible values) - for the details, Phil wrote a good post about this:
If you need fancier binding requirements, you can implement a custom model binder and explicitly define how route values and the other bits get translated into objects (or collections of objects).
Related
I have such action:
[ObjectRequired]
public ActionResult Campaign(int? id, SomeClass object = null)
{
...
}
What i need is to route to this action:
a) with only int parameter (.../Campaign/12345)
b) with no parametes (optionally) (.../Campaign)
MVC error says, that there is no nonparametric constructor (if delete "object" parameter - it's ok). But i cant delete "object" parameter, because i need to check some values and pass value from [ObjectRequired] attribute like this:
filterContext.ActionParameters["object"] = _someObject;
I don't want to use constructions like ViewData. Where's the right way?
I'm not 100% on what I'm about to say but I'll say it as if I am...
You can't pass an object through to a get. You could pass back parameters and use a custom route handler to build the object for you.
You could also hold the object in data and use Entity Framework to get the model.
You could also use TempData, but that's similar to ViewData.
You could also hold it in their Session.
I had to pass an extra parameter with my action links to indicate where they came from (as I needed to change a back link in the pages accordingly).
As it was a controller name, I decided to name it controller.
e.g. a sample link might be:
#Html.ActionLink(item.Name, "Options", "Questionnaire", new {
id = item.QuestionnaireId,
controller = "templates" }, null)
The receiving action in QuestionnaireController looked like:
public ActionResult Options(int id, string controller)
When the action was hit I noticed the controller value was not template, but instead was the name of the current controller (i.e. QuestionnaireController).
As an experiment I added an action parameter e.g.:
public ActionResult Options(int id, string controller, string action)
the action value was the current action too (i.e. Options).
My work-around for this was simply to rename my parameter to source, but why does MVC bother to map the names controller and action to action parameters? I assume that would apply to any/all Route Mapping values, but what is the purpose of this?
Why does MVC bother to map the names controller and action to action parameters?
I believe it's done as part of the QueryStringValueProvider or one of the other ValueProviders (maybe the RouteDataValueProvider). ASP.Net MVC uses Convention over Configuration, so the framework uses the values provided to populate method parameters. The Controller name, Action name and even the Area name are all values provided in the Url.
I assume that would apply to any/all Route Mapping values, but what is the purpose of this?
The ValueProvider is used for Routing data to determine the matching route to use, it also happens to be the same object that provides the data to populate method parameters. The side affect you are experiencing is most likely not a feature they were trying to implement.
The DefaultModelBinder.GetValue uses the ValueProviders to locate a value and bind it to the model (or method paramater).
I'm sending 2 different models to the same action, for eg. I'm either sending the ContactEdit or GeneralEdit model to the same action. The action will need to determine which model is sent. Is there a way to do this? I have no problem passing a query param to tell which model was passed, but is there a way to do something like:
[HttpPost]
public ActionResult SingleUser(Part part)
{
if(part == Part.General)
GeneralEditModel model = Model as GeneralEditModel;
else
ContactEditModel model = Model as ContactEditModel;
//....
}
You can name your elements and use a bind prefix. I believe if your method takes two parameters as two object types, the one will just be null if it's not found. See
MVC - Model binding with multiple entities on the same page
I have a model with a property that points to a file that contains HTML. My strongly typed view to this model uses a custom HTML helper method to resolve and return the HTML from the file. Works great so far.
The HTML read from each file will contain various controls whose values I need to retrieve when the form is POSTed.
What would be the best way to have access to the POSTed control values in my controller method?
I would prefer a non jQuery solution, but I am not sure if the MVC framework can provide these values to me? Can it provide a list of key/value pairs to the controller somehow?
You could use the FormCollection in ASP.NET MVC.
public ActionResult SomeAction(FormCollection form) {
...
}
You have essentially two options.
1) Use the old fashioned Request variables as all we have done in ASP.NET web forms.
For example in your controller action method you can retrieve any value present on the form with the following method
public ActionResult SomeAction() {
var request = this.ControllerContext.HttpContext.Request;
bool boolParam = bool.Parse( request["boolParam"] ?? "false" );
}
2) Create a custom Model Binder to let the framework pack those values in a custom class object.
This method would be a little bit more difficult at the beginning because you have to create a custom Model Binder but it favour readability on your controller code. For further details on creating custom model binders give a look at the following links (you can find more with a simple search)
Custom Model Binder for Complex composite objects
Custom Model Binder and More UI Validation in ASP.NET MVC
A Custom ASP.NET MVC Model Binder for Repositories
Hope it helps
Is the content of the HTML files dynamic or known at design time? If you know it now, you could have each one post to it's own action and then strongly type the parameters.
Is there a class in the ASP.NET MVC framework that represents a controller name and an action name?
I'm writing a method that will return a collection of URLs and I want them to be returned as objects that contain a 'Controller' and 'Action' property or something similar.
This is because sometimes I'll need to isolate just the controller name or just the action name.
There's the Route object, but it seems way too heavyweight for what I'm trying to do.
Is there a simpler abstraction in ASP.NET MVC?
There is a Controller Base class not a name though, an action name is simply an string which the ActionInvoker will use to find the correct action via reflection.
I think you'll have to use the Route Values Dictionary of key/value pairs to represent the route.
RouteValueDictionary myValues = new RouteValueDictionary();
myValues.Add("Controller", "myController");
You could use a normal dictionary and then convert it in code to a RouteValueDictionary if you don't want/can't have to have access to the Routing namespace.
In this approach you can then using the keys of the either a standard dictionary or the routeValue Dictionary to do the isolation of the controller or action.
String ControlString = myValues["Controller"]
Then do what you want with it. Maybe use constants for the keys you wish to access.