how to remove length from my url in asp.net mvc? - 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.

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.

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

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.

ViewModel not storing values when Ajax.ActionLink calling controller

When I'm clicking ActionLink and setting ViewModel values in controller I can see changes when View being rendered. But same values comes as null into Controller when I'm clicking ActionLink second time.
How do I store the value, so it comes into controller ?
View:
#Ajax.ActionLink("Click me", "AjaxTest", "Controller", new AjaxOptions()
{
UpdateTargetId = "updatePanel",
HttpMethod = "POST",
OnSuccess = "A()"
})
<div id="updatePanel">
#Html.Partial("~/Views/Shared/_UpdatableContent.cshtml", this.Model)
</div>
Controller:
[HttpPost]
public ActionResult AjaxTest(MyViewModel model)
{
model.A = "A"
return PartialView("_UpdatableContent", model);
}
Partial view _UpdatableContent:
#Html.HiddenFor(x => x.A)
#if (Model.A == "A")
{
//Draw
}
Try adding this.Model to your ActionLink following:
#Ajax.ActionLink("Click me", "AjaxTest", "Controller", this.Model, new AjaxOptions() { UpdateTargetId = "updatePanel" })
This method passes the model back into the request, which should allow the update to happen.
Probably my biggest gripe with ASP.NET MVC is the fact that the various "Helper" functions are overloaded to the nth-degree, and not always consistently in terms of the order the arguments appear...
Hope that helps :)
I had this very same problem. Setting HttpMethod = "Post" in the AjaxOptions fixed it for me, thanks Sergejs.
My final, working code is as follows
#{
AjaxOptions ajaxOptions = new AjaxOptions
{
HttpMethod = "Post",
LoadingElementId = "product-adding-" +#Model.Product.Id,
LoadingElementDuration = 100,
OnSuccess = "AddedToCart"
};
}
<div>
#Ajax.ActionLink("Add to cart",
"AddToCart",
"Cart",
new { id = Model.Product.Id, returnUrl = Request.Url.PathAndQuery },
ajaxOptions,
new { #class = "button" })
<img id="product-adding-#Model.Product.Id" src="~/Images/ajax_loader.gif" />
</div>

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