I am using MVC and my requirement is that i need to build a action link with dynamic action route value.
for example.
In my model i have the list<string> so action link should be.
../ActionMethodName/ControllerName? Item[0]=model.item[0]&Item[1]=model.Item[1]...so on.
Any Suggestion is welcome.
Several ways.
1) You can pass as array and access it.
3) You can use foreach with razor syntex to generate this type of link.
Initally , the parameter needs to be passed are hardcoded than OnClick of somthing , replace the queryString values .
#Ajax.ActionLink("Test", "actionName", "ControllerName", new { foo = "foo1", name = "ABC" }, new AjaxOptions { UpdateTargetId = "TargetName" }, new { id = "Test" })
and
$("#btnTest").click(function () {
// Replace the hardcoded parameter value desired one ... :)
$("#Test").attr('href', $("#Test").attr('href').replace("foo1", "XXX"))
});
Note:- this solution i read from asp.net forums
http://forums.asp.net/t/1841591.aspx?+pass+Javascript+value+parameter+with+Ajax+ActionLink
Related
I have a form generated through Razor:
using (Ajax.BeginForm("SaveProfile", "Settings", new { AccountID = Model.AccountID },
new AjaxOptions { HttpMethod = "Post" }))
The RouteValues object, new { AccountID = Model.AccountID } gets shoved into the Query String, even though I don't specify to do so anywhere in my project.
How can I pass these values to my controller action without letting them show up in the URL?
MVC model-binding will recognize values passed by the query-string or the form (post) variables.
So you could include hidden inputs in the form with the var names as the name attributes and the values as the value attributes:
#Html.HiddenFor(model => model.AccountID)
I have a view that renders a list of news, in that i have an href tag, and i need to call a Detail method and pass it the news id, now i have a querystring in my url like that
News/Details?id=x... but i need something like News/Details/Category/Title-of-something, a friendly url with news category, name and without the id
this is my action, it works but i get that querystring
foreach (var item in Model)
{
Read More
}
I was trying with something like, with a Url.RouteUrl
routes.MapRoute(
name: "Details",
url: "{controller}/{action}/{id}/{category}/{newsName}",
defaults: new { controller = "News", action = "Details"}
);
but it never goes to Details actionresult, and also i need to pass the id parameter for showing some news Details, but i don't want to display it in the friendly url. Im really confused how to achieve it. Thanks in advance
Try this:
#Html.RouteLink("Read More", "Details", new { action = "Details", id = item.newsId, category = item.categoryName, newsName = item.newsName })
Use :
Read More
Make sure your route is not overridden by other route. So put your route first in RoutConfig.cs
Change your code to:
foreach (var item in Model)
{
Read More
}//change name=item.newsName to newsName=item.newsName
Also make sure that your controller has correct signature as per your routing details:
public class NewsController : Controller
{
public ActionResult Details(int id, string category, string newsName )
}
First URL Correct
Read More
You may Route Debugger whenever yo see issue related to routes
I'm trying to learn asp.net mvc, and almost everywhere I see route description with three components like /Controller/Action/{anyParams}
I'd like to know if I can map a route similar to,
/Folder(or namespace)/Controller/Action/params...
ex:
/Admin/Student/Edit/id
/ABC/Faculty/Add/`
/XYZ/Student/Edit/id
or in general,
/XYZ/Controller1/Action/{param}
Yep the second parameter in the MapRoutes function (usually in Global.asax.cs is Url and this can be any pattern you want. something like
routes.MapRoute("MyRoute", "XYZ/Controller1/Action/{param}",
new {controller = "Controller1", action = "Action"}});
should do the trick.
You can make your routes as complex as you want.
F.e. the following route:
routes.MapRoute("some-route", "products/detail/order/{id}/{name}/",
new { controller = "Products", action = "Order" },
new { id = "^\d+" });
will route to the following function:
public class ProductsController : Controller {
public ActionResult Order (int id, string name) {
}
}
So you can specify as many parameters as you want, and they will be passed into your action as function parameters.
I'm trying to implement some simple paging, based on How do I do pagination in ASP.NET MVC?
The paging works fine.
However, I'm now trying to create previous and next links, but can't figure out how to access the params:
My route looks like:
routes.MapRoute(
"Name",
"Controller/ActionName/{pageID}",
new { controller = "Controller", action = "ActionName" , pageID = 0 },
new { pageID = #"\d*"}
);
And my next link looks like:
<%=Html.ActionLink("next page", "ActionName", "Controller", new {pageID = pageID + 1 }, null) %>
The error I get is:
Compiler Error Message: CS0103: The name 'pageID' does not exist in the current context
How should I create the Prev/Next links (or, in this case, just the next)?
The error is occurring on the second PageID in
new {pageID = pageID + 1 }, ...
If you want to reference pageID in this way, you have to pass it in as part of your model.
Have a look at the following tutorial:
NerdDinner Step 8: Paging Support
http://nerddinnerbook.s3.amazonaws.com/Part8.htm
I have a droplist, and I would like to add an All as the first one.
How is that done with asp.net mvc?
I have:
<%= Html.DropDownList("selCustomerID") %>
works great, but I don't have an empty "all-option". I populate it like this:
return View("Index", new CustomerAdminEditViewModel { selCustomerID = new SelectList(_cs.GetCustomers(), "CustomerID", "CustomerName") });
Check this link : http://forums.asp.net/t/1429075.aspx
It is talking about an optional label parameter in the constructor.