ASP.NET MVC 5 Actionlink ignores actionname - asp.net-mvc

This:
#Html.ActionLink(linkText: Txt.Get("rsTerugNaarOverzicht"),
actionName: "Index",
controllerName: "OfferteOverzicht",
routeValues: new { is10Days = true, maand = Model.OverzichtMaand, jaar = Model.OverzichtJaar },
htmlAttributes: new { #class = "wijzigen" })
is rendered as:
<a class="wijzigen" href="/OfferteOverzicht?is10Days=True&maand=3&jaar=2021">Terug naar overzicht</a>
I was expecting this:
<a class="wijzigen" href="/OfferteOverzicht/Index?is10Days=True&maand=3&jaar=2021">Terug naar overzicht</a>
What am I doing wrong here?

Resolved it.
It was caused by an entry in the routeconfig:
routes.MapRoute(
name: "Root",
url: "{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I needed to remove the first one.
Now, eventhough not visible in the link, the action is called and executed.

Related

how to remove length from my url in asp.net mvc?

I m creating an Mvc application . I have some issue .
I am getting url
http://localhost:2355/Home/Contract?Length=4
I want my url as
http://localhost:2355/Home/Contract
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
what is problem in my route mapping
If you're using #Html.ActionLink() you should try try these ones.
#Html.ActionLink("MyAction", "MyController", new { }, new { #Class="class-name"})
with Areas
#Html.ActionLink("MyAction", "MyController", new { }, new {#Area = "MyArea" #Class="class-name"})
Sendind data
#using (Ajax.BeginForm("MyAction", "MyController", new { }, new AjaxOptions { HttpMethod = "POST" }, new { #Area = "MyArea" }))
Sending data with no Area
#using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions { HttpMethod = "POST" }));
Besides that, you can check the url which #gardarvalur has posted.
I hope it helps you.

Kendo UI doesnt call Action Result

I use Kendo UI in ASp.NET 5 . I have a Action Method To show Data . its :
I do not add any javascript code , Do Need?
public virtual ActionResult ReadData([DataSourceRequest] DataSourceRequest request)
{
request.Page = 1;
IEnumerable<ShowProvinceVM> ddd = _provinceService.GetAll().Select(x => new ShowProvinceVM { Id = province.ProvinceId, Name = province.ProvinceName, NameEn = province.ProvinceNameEn, Code = province.ProvinceCode }).ToList();
DataSourceResult result = ddd.ToDataSourceResult(request);
result.Total = 20;
return Json(result, "text/x-json", JsonRequestBehavior.AllowGet);
}
And My Helper for show grid data is:
#(Html.Kendo().Grid<CMS.ViewModel.Province.ShowProvinceVM>
()
.Name("grid2")
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("ReadData", "Province"))
)
.Columns(columns =>
{
columns.Bound(c => c.Id);
columns.Bound(c => c.Name);
})
.Pageable()
.Sortable()
)
But when I run Project , Its empty and dont call Action Methods.
Whats Problem ?
When I pass Data from Action Index to View and edit Grid like this , It show Data :
#(Html.Kendo().Grid(Model) //Bind the grid to ViewBag.Products
.Name("grid")
.Columns(columns =>
{
// Create a column bound to the ProductID property
columns.Bound(product => product.Id);
// Create a column bound to the ProductName property
columns.Bound(product => product.Name);
})
.Pageable() // Enable paging
.Pageable()
.Sortable()
My routing :
routes.MapRoute(
name: "lang",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "CMS.mvcApp.Controllers" }
);
routes.MapRoute(
name: "langvalueparam",
url: "{lang}/{controller}/{action}/{value}",
defaults: new { controller = "Home", action = "Index", value = UrlParameter.Optional }, namespaces: new string[] { "CMS.mvcApp.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "CMS.mvcApp.Controllers" }
);
routes.MapRoute(
"AdminDefault",
"Admin/{controller}/{action}/{id}",
new { area = "Admin", controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] { "CMS.mvcApp.Areas.Admin.Controllers" });
routes.MapRoute(
"AdminPage",
"Admin/{controller}/{action}/{page}",
new { area = "Admin", controller = "Home", action = "Index", page = UrlParameter.Optional }, namespaces: new string[] { "CMS.mvcApp.Areas.Admin.Controllers" });
Please be aware that kendo send it's read request as POST!
You need to define the request type if you want to use GET (which it seems as you use JsonRequestBehavior.AllowGet)
.Read(read => read.Action("ReadData", "Province").Type(HttpVerbs.Get))

MVC - Ajax.BeginForm() generating empty action

Current page URL: http://localhost:25265/SearchResultsList.aspx
view looks like:
#using (Html.BeginForm("RefineSearchResults", "Search", FormMethod.Post, new {id = "myForm"}))
{
<input type="submit" value="submit" />
}
Routes:
routes.MapRoute(
"Search",
"SearchResultsList.aspx",
new { controller = "Search", action = "SearchResults" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index"},
namespaces: new[] { "MyApp.WebUI.Controllers" }
);
But, I noticed that it's generating empty action.
looks like:
<form action method="post">
<input type="submit" value="submit"> </form>
Can anyone please tell me what's this happening? Where I'm doing wrong!!

ASP.NET MVC Search Results Page MapRoute Doesn't Work

How can i set mapRoute for search results page? My code doesn't work.
Global.asax.cs
routes.MapRoute(
name: "SearchResults",
url: "{action}/{Keyword}",
defaults: new { controller = "Home", action = "Search" }
);
Search Form
#using (Html.BeginForm("Search", "Home", FormMethod.Get))
{
#Html.TextBox("Keyword",null , new { #class = "SearchBox" })
<input type="submit" value="Search" />
}
HomeController.cs
public ActionResult Search(string Keyword)
{
GamesContext db = new GamesContext();
var SearchResults= (from i in db.Games where i.GameName.Contains(Keyword) || i.GameDesc.Contains(Keyword) select i).Take(20).ToList();
return View(SearchResults.AsEnumerable());
}
This one works for me (should be before default route):
routes.MapRoute(
"SearchResults",
"Search/{Keyword}",
new { controller = "Search", action = "SearchAction" }
);
Creating an ActionLink and MapRoute that there is a constant name in it
And there's a point to use new controller for search instead of home with this route.

ASP MVC url/link generation on Area produces empty

I am using ASP MVC 5 and trying to generate a link in my view inside the "ControlPanel" Area
my view rootfolder/Areas/ControlPanel/Views/CourseCategory/Index.cshtml
#Html.ActionLink("edit", "Edit", new { id = item.Id }, new { #class = "btn btn-default btn-xs" })
#Html.ActionLink("delete", "Delete", new { id = item.Id }, new { #class = "btn btn-danger btn-xs" })
my AreaRegistration
public class ControlPanelAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "ControlPanel";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.LowercaseUrls = true;
context.MapRoute(
name: "ControlPanel.CourseCategory",
url: "controlpanel/course-categories/{id}",
defaults: new { controller = "CourseCategory", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Website.Areas.ControlPanel.Controllers" }
);
}
}
RouteConfig file
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "HomePage",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
}
}
my controller
namespace Website.Areas.ControlPanel.Controllers
{
public class CourseCategoryController : ControlPanelController
{
public CourseCategoryController(IUnitOfWork uow)
:base(uow)
{
}
public ActionResult Index()
{
return View(_uow.Repository<CourseCategory>().GetAllOrderByCode());
}
}
}
now the output html produces empty href
<a class="btn btn-default btn-xs" href="">edit</a>
<a class="btn btn-danger btn-xs" href="">delete</a>
what is the correct way of generating link on Html.ActionLink or Url.Action and Url.RouteUrl in this case?
It looks like you are missing the controller in your route values (I noticed you are not specifying one anywhere in your #Html.ActionLink)
#Html.ActionLink("edit", "Edit", new { controller="CourseCategory", id = item.Id }, new { #class = "btn btn-default btn-xs" })
Or - an alternative overload of the Html.ActionLink
#Html.ActionLink("edit", "Edit", "CourseCategory", new { id = item.Id }, new { #class = "btn btn-default btn-xs" })
Lastly, it never hurts to include the area name as part of the RouteValues dictionary, but this is not required.
EDIT
It looks like you updated your question with some additional information. This does not look right (having a blank value for URL)
routes.MapRoute(
name: "HomePage",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
By default, it should look something like this
routes.MapRoute(
name: "HomePage",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
I would assume having a blank URL is definitely something that could cause a blank URL to be created in an Html.ActionLink. These helpers looks at your route config to generate the URL and this one seems to be taking over probably everything since it matches everything.
Try to do this area name along with id
#Html.ActionLink("edit", "Edit", new { id = item.Id, area = "ControlPanel" }, new { #class = "btn btn-default btn-xs" })
#Html.ActionLink("delete", "Delete", new { id = item.Id, area = "ControlPanel" }, new { #class = "btn btn-danger btn-xs" })
I think the problem is with the defaults which points to the Index action and action is not bound on the route!??

Resources