1

I am working on ASP.NET MVC project. I am facing one problem now. I am redirecting from one action to other when I click on Add list option in my drop down.

This is the script I am using for redirection :

 $("#DrpList").change(function (e) {
     if ($(this).val() == 'Add List') {
         document.location.href = 'http://localhost:1234/report/Index/indexid'

Here indexid is just random id which I am passing and it is static, So I can compare this in my Index controller and display view I need.

Here what I want is, after i pass indexid parameter to index, when Index page displays, I can see indexid in my link like this,

http://localhost:1234/report/Index/indexid

but i just need to display,

http://localhost:1234/report/Index 

I tried to do like :

return view("Index", "report", new{id = ""});

But it doesn't work. So how can I achieve this ?

Update :

 public ActionResult Index(string id)
    {
  if (id == "indexid")
  {
  //Here Add items to list
 return View("Index", "report", new { id = "" });
  }
Ajay
  • 317
  • 2
  • 12
  • 25

2 Answers2

0

Your Report Controller Index action signature should use indexid and not default id. Also indexid should be nullable

public class ReportController:Controller
{

   public ActionResult Index(string indexid)
   {

   }

}

and

 return view("Index", "report", new{indexid= ""});
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • Hi its not working. I already have string id which I am using in index. I think even if i change that to stringid, it doesnt make difference. I can see value indexid in id or stringid.But in return view, I am not able to make that null. I can see that in link – Ajay Feb 19 '14 at 10:29
  • @Ajay, please add your action method code, especially signature in your post. It is hard to understand with out it – Murali Murugesan Feb 19 '14 at 10:32
0

You can use ActionFilter to route as per your requirement. Create an ActionFilter as shown below -

public class MyFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var rd = filterContext.RouteData;
        if(rd.Values.Keys.Contains("id"))
        {
            filterContext.HttpContext.Items["key"] = rd.Values["id"];
            rd.Values.Remove("id");
            filterContext.Result = new RedirectResult("/Controller/Index");
        }
    }
}

Then use this Filter on the action where you are passing ID as the value -

    [MyFilter]
    public ActionResult Index()
    {
        return View();
    }

You can access id value in the Action using HttpContext.Items["id"].

When you request to a page with URL - /Controller/Action/indexid, it will be redirected to /Controller/Index

ramiramilu
  • 17,044
  • 6
  • 49
  • 66