标签: actionlink

如何链接到将数组作为参数的操作(RedirectToAction 和/或 ActionLink)?

我有一个这样定义的动作:

public ActionResult Foo(int[] bar) { ... }
Run Code Online (Sandbox Code Playgroud)

像这样的网址将按预期工作:

.../Controller/Foo?bar=1&bar=3&bar=5
Run Code Online (Sandbox Code Playgroud)

我有另一个动作可以做一些工作,然后重定向到Foo上面的动作以获得bar.

是否有一种使用 RedirectToAction 或 ActionLink 指定路由值的简单方法,以便像上面的示例一样生成 url?

这些似乎不起作用:

return RedirectToAction("Foo", new { bar = new[] { 1, 3, 5 } });
return RedirectToAction("Foo", new[] { 1, 3, 5 });

<%= Html.ActionLink("Foo", "Foo", new { bar = new[] { 1, 3, 5 } }) %>
<%= Html.ActionLink("Foo", "Foo", new[] { 1, 3, 5 }) %>
Run Code Online (Sandbox Code Playgroud)

但是,对于数组中的单个项目,这些确实有效:

return RedirectToAction("Foo", new { bar = 1 });
<%= Html.ActionLink("Foo", "Foo", …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc url-routing redirecttoaction actionlink asp.net-mvc-routing

5
推荐指数
1
解决办法
2528
查看次数

带有下拉列表中id值的Html.ActionLink

我有一个下拉列表:<%= Html.DropDownList("ddlNames",新的SelectList(Model.NameList,"ID","Name"))%>

我有一个ActionLink:<%:Html.ActionLink("编辑","编辑","成员",新{area ="MembersArea",id = XXX},null)%>

我想要XXX中下拉列表的值.所以我想在ActionLink的视图中使用控件的值.这有可能以简单的方式进行吗?

谢谢,

菲利普

actionlink asp.net-mvc-2 drop-down-menu

5
推荐指数
1
解决办法
2万
查看次数

MVC 3 Ajax.ActionLink无法正常工作

我正在玩一些基于MvcMusicStore示例的商店,并且在使用MVC3 Ajax.ActionLink/Ajax.RouteLink助手时遇到了一些问题.问题是它根本不生成Ajax请求(Request.IsAjaxRequest()== false).我使用Ajax.BeginForm/Ajax.BeginRouteForm生成的表单虽然很好用.

配置:

<appSettings>
    <add key="ClientValidationEnabled" 
         value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" 
         value="true"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

脚本:

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

链接:

@Ajax.ActionLink("show cart", "Show", "Cart", new AjaxOptions() { OnSuccess = "handleSuccess", HttpMethod = "Get", OnFailure = "handleFailure" })
Run Code Online (Sandbox Code Playgroud)

生成HTML:

<a data-ajax="true" data-ajax-failure="handleFailure" data-ajax-method="Get" data-ajax-success="handleSuccess" href="/Cart/Show">show cart</a>
Run Code Online (Sandbox Code Playgroud)

如上所述,这很好用:

@using (Ajax.BeginForm(
        "Show",
        new { controller = "Cart" },
        new AjaxOptions
        {
            OnSuccess = "handleSuccess",
            OnFailure = "handleFailure"
        }))
    {
        <input type="submit" class="button" />
    }
Run Code Online (Sandbox Code Playgroud)

该动作如下所示:

[Authorize]     
public …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-ajax unobtrusive-javascript actionlink asp.net-mvc-3

5
推荐指数
1
解决办法
1万
查看次数

将参数传递给MVC Ajax.ActionLink

如何将TextBox的值作为ActionLink的参数发送?

我需要使用Html.TextBoxFor

<%= Html.TextBoxFor(m => m.SomeField)%>
<%= Ajax.ActionLink("Link Text", "MyAction", "MyController", new { foo = "I need here the content of the textBox, I mean the 'SomeField' value"}, new AjaxOptions{ UpdateTargetId = "updateTargetId"} )%>
Run Code Online (Sandbox Code Playgroud)

Contoller/Actions看起来像这样:

public class MyController{
   public ActionResult MyAction(string foo)
   {      
      /* return your content */   
   }
}
Run Code Online (Sandbox Code Playgroud)

使用MVC 2.0

parameters asp.net-ajax actionlink asp.net-mvc-2

5
推荐指数
1
解决办法
1万
查看次数

为什么#字符被添加到网址?

我有一个显示缩略图列表的简单页面,

我想添加一个寻呼机,我添加了两个动作链接

  <li class="ui-pagination-next">@Html.ActionLink("Next", "Paginate", "Home", new { nbSkip = ViewBag.nextSkip }, null)</li>
Run Code Online (Sandbox Code Playgroud)

当我点击我重定向到的链接时

http://localhost:41626/#/Home/Paginate?nbSkip=60
Run Code Online (Sandbox Code Playgroud)

代替

http://localhost:41626/Home/Paginate?nbSkip=60 , 
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么'#'字符被添加到URL?

路线copnfig:

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 }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-mvc actionlink asp.net-mvc-4

5
推荐指数
1
解决办法
840
查看次数

MVC Html.ActionLink 从 URL 中删除空的查询字符串参数

我正在使用Html.ActionLink(string linkText, string actionName, object routeValues)重载将一些参数发送到操作方法..

有时我需要传递一个空参数值,如: ?item1=&item2=value

我指定的匿名类型创建,并通过作为帕拉姆routeValues,但两者nullstring.Empty导致URL省略了空item1PARAM。

new { item1 = null, item2 = "value" } 
new { item1 = string.Empty, item2 = "value" }
new { item1 = "", item2 = "value" }

// all result in:
?item2=value
Run Code Online (Sandbox Code Playgroud)

我可以手动创建 URL,但我想使用辅助方法。

建议?

parameters asp.net-mvc actionlink query-string razor

5
推荐指数
1
解决办法
1850
查看次数

MVC ActionLink生成不同类型的链接......为什么?

我是MVC的新手,并且有一个我正在努力的应用程序.我有2个不同的链接,在我的全球范围内有2条路线,这些路线相当类似

路线1

routes.MapRoute("Category", "Movies/{category}/{subcategory}", 
    new { controller = "Catalog", action = "Index", category = "", subcategory = "" });
Run Code Online (Sandbox Code Playgroud)

路线2

routes.MapRoute("Movie", "Movie/{movie}", 
    new { controller = "Movie", action = "Index", movie = "" });
Run Code Online (Sandbox Code Playgroud)

当我为第一条路线调用一个动作链接时,它会创建它,因为我认为它应该:

.../Movies/Category/SubCategory

但是,当我创建我的第二个链接时,它填充它像这样:

.../Movie?movieId=ff569575-08ec-4049-93e2-901e7b0cb96a

之前我使用的是字符串而不是guid,它仍然在做同样的事情

.../Movie?movieName=Snatch

我的actionlinks设置如下

<%= Html.ActionLink(parent.Name, "Index", "Catalog",
    new { category = parent.Name, subCategory = "" }, null)%>

<%= Html.ActionLink(movie.Name, "Index", "Movie", 
    new { movieId = movie.MovieId }, null)%>
Run Code Online (Sandbox Code Playgroud)

我的应用程序仍然有效,但我认为这种行为很奇怪.任何帮助都会很棒.

谢谢!

c# asp.net-mvc actionlink

4
推荐指数
1
解决办法
5583
查看次数

mvc ActionLink中的RouteValueDictionary/object问题

所以这里:

我有一个html帮助器,它ActionLink使用当前url的可选参数呈现s.这个html帮助器还允许您根据需要添加一些可选参数,并将它们合并为1 RouteValueDictionary.

    public static string ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs, object htmlAttributes) {

        //get current optional params from current URL
        NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

        //put those in a dict
        RouteValueDictionary r = new RouteValueDictionary();
        foreach (string s in c.AllKeys) {
            r.Add(s, c[s]);
        }

        RouteValueDictionary htmlAtts = new RouteValueDictionary(htmlAttributes);

        RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

        //merge them
        RouteValueDictionary m = RouteValues.MergeRouteValues(r, extra);

        return helper.ActionLink(linktext, action, controller, m, htmlAtts).ToHtmlString();
    }
Run Code Online (Sandbox Code Playgroud)

这很完美,但我现在添加了SecurityAware Actionlinks.

所以 …

asp.net-mvc actionlink routevalues

4
推荐指数
1
解决办法
7553
查看次数

RouteValueDictionary和htmlAttributes之间是否存在冲突?

我使用RouteValueDictionary将RouteValues传递给ActionLink:

如果我编码:

<%:Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, null)%>
Run Code Online (Sandbox Code Playgroud)

链接结果是Ok:

SearchArticles?refSearch=2&exact=False&manufacturerId=5&modelId=3485&engineId=-1&vehicleTypeId=5313&familyId=100032&page=0
Run Code Online (Sandbox Code Playgroud)

但如果我编码:

<%: Html.ActionLink(SharedResources.Shared_Pagination_First, Model.ActionToExecute, Model.ControllerToExecute, Model.FirstRouteValues, new { @title = string.Format(SharedResources.Shared_Pagination_LinkTitle, 0) })%>
Run Code Online (Sandbox Code Playgroud)

链接结果是:

SearchArticles?Count=10&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D
Run Code Online (Sandbox Code Playgroud)

有什么问题?唯一的区别是,在最后我使用的是htmlAttributes

c# asp.net-mvc html-helper actionlink routevalues

4
推荐指数
1
解决办法
1656
查看次数

将图像附加到MVC 4中的ActionLink

我在MVC 4应用程序中使用带有id的ActionLink,并在css中为actionLink id指定一个图像,但在地球上我做错了.不管用!这是我的代码

 <div class="logo_container">
            @Html.ActionLink(" ", "Index", "Home", null, new { id = "_Logo" })
 </div>
Run Code Online (Sandbox Code Playgroud)

CSS

.logo_container {
width:339px;
height:116px; 
}

#_Logo {
background-image: url("../Images/logo.png");
 width:339px;
height:116px;
background-color:green;
}
Run Code Online (Sandbox Code Playgroud)

css asp.net-mvc actionlink razor

4
推荐指数
3
解决办法
2万
查看次数