带有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 = 我在这里缺少什么?我刚开始学习MVC 欢迎提出每一个提示。
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.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);
}
...