Kendo UI doesnt call Action Result - asp.net-mvc

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))

Related

ASP.NET MVC 5 Actionlink ignores actionname

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.

ASP.NET MVC controller not receiving the parameter from view

I am getting null value for the actor(which is an action parameter). I tried all possible ways to call the action method. I don't know if there is any setting in the web.config or what. When I try it in sample MVC application, it worked fine(getting parameters as expected). But the same thing is not working in my current working project. Please help me.
Html:
<table>
<tr>
<th>Time Info</th>
<th>Actor</th>
<th>Reset</th>
</tr>
#{
List<LockedUser> lockedUsers = ViewBag.LockedUsers;
foreach (LockedUser lockedUser in lockedUsers)
{
<tr>
<td>#lockedUser.TimeInfo</td>
<td>#lockedUser.Actor</td>
<td>#Html.ActionLink("Reset", "Reset", "Admin", new { actor = "John" }, null)</td></tr>
}
}
</table>
Action in AdminController:
public ActionResult Reset(string actor)
{
if (System.Web.HttpContext.Current.Cache.Get(actor) != null)
{
System.Web.HttpContext.Current.Cache.Remove(actor);
Debug.WriteLine("Reset Successfull");
ViewBag.Message = "Reset Successfull";
}
else
{
ViewBag.Message = "Unable to reset";
}
return View();
}
RouteConfig:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{WCP}",
defaults: new { controller = "EmailConfirm", action = "Index", WCP = UrlParameter.Optional }
);
Try this:
<td>#Html.ActionLink("Reset", "Reset", "Admin", new { WCP= "John" }, null)</td></tr>

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.

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.

MVC Html.BeginForm using Areas

I'm using MVC areas and on a view that's in an area called "Test" I would like to have a form that posts to the following method:
area: Security
controller: AccountController
method: logon
How can I make this happen with Html.BeginForm? Can it be done?
For those of you that want to know how to get it to work with the default mvc4 template
#using (Html.BeginForm("LogOff", "Account", new { area = ""}, FormMethod.Post, new { id = "logoutForm" }))
Try this:
Html.BeginForm("logon", "Account", new {area="Security"})
Try specifying the area, controller, action as RouteValues
#using (Html.BeginForm( new { area = "security", controller = "account", action = "logon" } ))
{
...
}
Use this for area with HTML Attributes
#using (Html.BeginForm(
"Course",
"Assign",
new { area = "School" },
FormMethod.Get,
new { #class = "form_section", id = "form_course" }))
{
...
}
#using (Html.BeginForm("", "", FormMethod.Post, new { id = "logoutForm", action = "/Account/LogOff" }))
{#Html.AntiForgeryToken()
<a class="signout" href="javascript:document.getElementById('logoutForm').submit()">logout</a>
}
For Ajax BeginForm we can use this
Ajax.BeginForm("IndexSearch", "Upload", new { area = "CapacityPlan" }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = updateTarget }, new { id = "search-form", role = "search" })

Resources