mvc4 @html.action,带有QueryString参数的@ Html.Action MVC 4(@Html.Action MVC 4 with QueryString Parameters...

带有QueryString参数的@ Html.Action MVC 4(@Html.Action MVC 4 with QueryString Parameters)

我有一个与MVC 4中的Html.Action相关的问题我想将一些Querystring变量传递给Details视图

我现在的代码是

System.Text.StringBuilder MobileData = new System.Text.StringBuilder();

MobileData.AppendFormat("{1:dd-MM-yyyy}", tk.ID, tk.Datum);

问题是他会将我重定向到localhost / PROJECTNAME / Home / Taken_Detail / 2我想要的是Home / Taken_Detail?id = 2我在这里缺少什么我刚刚开始学习MVC 4,欢迎提出每个提示。

I have a question related to the Html.Action in MVC 4 I want to pass some Querystring Variables with it to the Details view

The code I have now is

System.Text.StringBuilder MobileData = new System.Text.StringBuilder();

MobileData.AppendFormat("{1:dd-MM-yyyy}", tk.ID, tk.Datum);

The problem is he would redirect me to localhost/PROJECTNAME/Home/Taken_Detail/2 what I want is Home/Taken_Detail?id=2 what am I missing here I am just starting to learn MVC 4, Every tip is welcome.

原文:https://stackoverflow.com/questions/16666679

更新时间:2019-11-18 16:55

最满意答案

这是因为您的路由包含id参数。 从路由中删除它, Url.Action将更改URL并将您的参数添加到查询字符串。

例:

routes.MapRoute("Default", "{controller}/{action}/{id}",

new { controller = "Home", action = "Index", id = UrlParameter.Optional });

如果使用Url.Action指定id参数,则将在最后一个斜杠之后放置该参数。

如果你删除它:

routes.MapRoute("Default", "{controller}/{action}",

new { controller = "Home", action = "Index" });

生成的URL将包含一个包含id的查询字符串。

This is because your routes contain the id parameter. Remove it from the routes and Url.Action will change the URL and add your parameter to the query string.

Example:

routes.MapRoute("Default", "{controller}/{action}/{id}",

new { controller = "Home", action = "Index", id = UrlParameter.Optional });

The id parameter will be put after the last slash if you specify it with Url.Action.

If you remove it:

routes.MapRoute("Default", "{controller}/{action}",

new { controller = "Home", action = "Index" });

The resulting URL will have a query string containing the id.

相关问答

一、Html.Ation是不能加载分部页的,一般是用于连接跳转 二、分部视图加载是用@Html.Partial("分部视图名",值)

您应该查看Action方法的文档; 这很好解释。 对于你的情况,这应该工作: @Html.Action("GetOptions", new { pk="00", rk="00" });

controllerName参数将默认为调用Html.Action的控制器。 所以如果你试图从另一个控制器调用一个动作,你必须像这样指定控制器名称: @Html.Action("GetOptions", "ControllerName", new { pk="00", rk="00" });

You shoul

...

我认为这篇博文将帮助你更多地理解: http://blogs.charteris.com/blogs/gopalk/archive/2009/01/20/how-does-asp-net-mvc-work.aspx 。 从本质上讲,路由参与确定哪个控制器'启动'来处理请求,并根据您发送的参数和MVCRouteHandler使用这些参数来做出决定的适当操作。 仅仅因为你告诉它你的行为中的哪个控制器不会让它神奇地忽略路由表,就直接去找那个控制器类,并绕过在后端发生的所有其他MVC优点。 请记住,这些@

...

我肯定肯定会错过一些东西,但是我通过注册每个Plugin.ProductController动作路径来绕过我的问题,我已经从管理员中覆盖了这些路线。 routes.MapRoute("Route1",

"Admin/Product/{action}/{id}",

new { controller = "Product", action = "Index", area = "Admin", id = UrlParameter.Optional },

new[] { "Plug

...

这是因为您的路由包含id参数。 从路由中删除它, Url.Action将更改URL并将您的参数添加到查询字符串。 例: routes.MapRoute("Default", "{controller}/{action}/{id}",

new { controller = "Home", action = "Index", id = UrlParameter.Optional });

如果使用Url.Action指定id参数,则将在最后一个斜杠之后放置该参数。 如果你删除它: routes

...

这是我认为使用Html.Action或Html.Partial的指南 Html.Partial 当您呈现静态内容时使用Html.Partial , 如果要从发送到主视图的ViewModel传递数据 Html.Action 当您实际需要从服务器检索附加数据以填充部分视图时,请使用Html.Action 基本上,如果是静态的,使用Html.Partial() 。 如果动态,模型独立的数据,使用Html.Action() 。 可能有更多的场景,但这将给你一个很好的想法,在哪里/如何去。 Html.Ren

...

那么,开箱即用的MVC 3具有名为id的默认路由参数。 你的SubChild操作有一个名为val的参数,所以这可能是问题。 将Action中的参数重命名为id,或添加新路线 routes.MapRoute(

"SubChild",

"{controller}/SubChild/{val}",

new

{

controller = "ControllerName",

action = "SubChild",

val

...

@Html.Action正在执行指定的操作,并将该操作的结果作为字符串返回。 如果您正在重新渲染Index操作,然后重新渲染同一个视图,它只是圆形和圆形。 如果您想要链接,请改用@Html.ActionLink("Index") 。 以下是这种情况的一个例子: public class HomeController : Controller

{

public ViewResult Index()

{

return View();

}

}

这是Razor代码:

...

对象的类型可以是从原始类型(如int,string等)到自定义对象的任何类型。 如果您已为ViewBag分配了值,例如: public class CustomType {

public int IntVal { get; set; }

public string StrVal { get; set; }

}

...

ViewBag.SomeObject = new CustomType { IntVal = 5, StrVal = "Hello" }

您可以简单地调用它: @Html.

...

我将填充控制器中的数据,然后将其传递给模型,最后使用Html.DropDownListFor 。 我的意思是,这就是MVC所代表的:) 例 (PS:已经有一段时间了,因为我不再用c#编码了,所以对于任何类型的错字道歉) controller.cs Public ActionResult ()

{

Model m = new Model();

//populate data into a list

m.Items = ...

return View(m);

}

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值