passing url parameter array string to action method - asp.net-mvc

I want call action method with something like this
http://server/controller/action/?idsTypes=[1, type1]&idsTypes=[2, type2]
Action method
[HttpGet]
public ActionResult myMethod(string[][] idsTypes)
{
........
}
How change the url to pass the idsTypes parameter to method?
Thanks

Related

How to set default Controller and action method in Attribute routing in MVC5(Without using Convention based routing)

I am implementing attribute routing in MVC5 for my application.
In the application i have some controllers,and each of them having some action methods,Now i want to make one of my controller and action method as default for my application,without making the use of Convention based routing.
Example:
[RoutePrefix("Home")]
public class HomeController : Controller
{
[Route("Login")]
[HttpGet]
public ActionResult Login()
{
//Some body content
}
[Route("MyAccount")]
[HttpGet]
public ActionResult MyAccount()
{
//Some body content
}
}
Like that i have some other controllers and each of them has some action methods.
Now when i run my application,The first method which gets hitted is "Login" of the home controller,so in convention based routing it was hitting this method by default.But in Attribute Routing,i need to pass this controller name and action method http://{localhost}:8346/Home/Login.
So in the url i don't want to pass this Home controller name and Login action method.I want to make it default.So when i run my application it should hit this Login method.
How to achieve this.Any response will be greatly appreciated?
Try this
public class HomeController : Controller
{
[Route]
public ActionResult Login()
{
//Some body content
}
[Route("Home/MyAccount")]
public ActionResult MyAccount()
{
//Some body content
}
}

Value from view to action method using ActionLink

I am trying to pass value from view to action method using ActionLink
#Html.ActionLink("Click here", "GetvaluefromView", "ActionLink2", new { id = 3 })
My controller action method looks like below
[HttpPost]
public ActionResult GetvaluefromView(int ? Str )
{
return View("Methodname");
//http://localhost:1542/ActionLink2/Methodname
}
But this is not passing the value to action method.
Am I missing anything here?

ASP.NET MVC - RouteData set in filter action not bound to action method parameter

I have an action filter which (among other things), adds stuff to the RouteData. The value, however, is not picked up by the parameter in my action method. Any ideas why?
Action Filter:
public class SomeFilter : FilterAttribute, IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var someData = new SomeClass();
//do stuff
filterContext.RouteData.Values["someData"] = someData;
}
}
Action Method:
[SomeFilter]
public ViewResult SomeActionMethod(SomeClass someData)
{
//someData is null here
}
Please note that the following line inside my action method does return something the data saved into it in action filter:
SomeClass isNotNull = RouteData.Values["someData"] as SomeClass;
Anyone knows why?
The filter is attached to the action (method). Hence by the time the filter is run, the values for the parameters have already been chosen. Imagine the situation if what you asked worked:
[SomeFilter]
public ViewResult SomeActionMethod()
{
// ....
}
public ViewResult SomeActionMethod(SomeClass someData)
{
// .....
}
You reference http://mysite.com/mycontroller/SomeActionMethod with no query parameter. So then it should call the first action. But if your filter were to do what you wanted, after it ran, it should call the second action. But that one DOESN'T have the filter, so it should call the first. And round & round.
Here's an article that covers how to modify the Parameter values from an Action Filter:
http://haacked.com/archive/2010/02/21/manipulating-action-method-parameters.aspx/

overload action for number of parameters in asp.net mvc

Is it possible to overload the action methods based on number of parameters in request?
Eg:
1.
domain.com/List/Filter/ByName
invokes -> public ActionResult Filter(string criteria1)
2.
domain.com/List/Filter/ByName/ByRanking
invokes -> public ActionResult Filter(string criteria1, string criteria2)
I'm using asp.net mvc2.
Action methods cannot be overloaded based on parameters because there would be no reasonable way to disambiguate a URL into multiple overloaded methods.
What you can do, though is either this:
public ActionResult Filter(string criteria1, string criteria2)
and then check whether criteria2 is null to filter only by name.
Alternatively, you can use ActionNameAttribute to decorate your action methods
[ActionName("FilterByName")]
public ActionResult Filter(string criteria1)
[ActionName("FilterByNameAndRanking")]
public ActionResult Filter(string criteria1, string criteria2)
and then use that name in route registration. This approach, however, can lead to much confusion.
If I'm not mistaken the best way to do this would be to add two different controller methods and map them to two different Urls.
public ActionResult Filter1(string criteria1);
public ActionResult Filter2(string criteria1, criteria2);
Then you have two route definitions:
This will map this URL List/Filter/xxCriteria/ to the first controller
routes.MapRoute(
"Filter", // Route name
"{controller}/Filter/{criteria1}", // URL with parameters
new { controller = "List", action = "Filter1", criteria="" } // Parameter defaults
);
This will map this URL List/Filter/xxCriteriaName/xxxCriteriaRank to the second controller. Without this route you could still map a url to the second method, but it would look like : List/Filter/?criteria1=xx&criteria2=xx
routes.MapRoute(
"Filter2", // Route name
"{controller}/Filter/{criteria1}/{criteria2}", // URL with parameters
new { controller = "List", action = "Filter2", criteria1 = "", criteria2 = "" } // Parameter defaults
);
Hope it helped.

Is there a method or constructor that can be called prior to the requested action?

I have a controller that is all ajax calls, so I want to verify it's an ajax call once on the page before it calls the controller action so that if it's not an ajax call I can redirect to a home or error page. Is this possible? I know I can put in the constructor class, but the request object is null at that point.
You can use an ActionFilter. It's designed to accomplish this task:
public class MyActionFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext context) {
// will get executed when the decorated action runs
}
}
// Action method:
[MyActionFilter]
public ActionResult Index(int id) {
// ...
}

Resources