UrlHelper.Action is incorrectly url
-
Hello! I'm trying to redirect data from one controller's act to another operation of the same controller. The Json method is used. This is preceded by a url through UrlHelper.Action. Instead of the url parameter, there's something like that: "/?Length=2115." Please help address this problem. The counteraller code and RouteConfig.cs are listed below.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;
namespace Cars_project_3.Controllers
{public class HomeController : Controller { public ActionResult Index(string id) { var repository = new Repository(); ViewBag.ViewbagValues = repository.GetAllCustomersAndOrders(); Response.Write(id); return View(repository.GetAllCustomersAndOrders()); } [HttpPost] [ValidateInput(false)] public ActionResult GetData(string d) { var id = new { URL = Url.Action("Index", "Home", d) }; return Json(id, JsonRequestBehavior.AllowGet); } }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;namespace Cars_project_3
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
-
The mistake is:
Url.Action("Index", "Home", d)
♪ The third parameter is not a parameter, but an object whose properties are a parameter.That's right.
var id = new { URL = Url.Action("Index", "Home", new { id = d }) };