如何在.Net MVC中将多个参数从视图传递到控制器

时间:2016-02-08 05:57:28

标签: asp.net-mvc

我需要在click事件中将2个参数id和date从视图传递到控制器端。这可能是基本问题,但我无法这样做。

我只是尝试了这段代码

<flow name="HelloWorld" doc:name="HelloWorld">
    <!--<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8888" doc:name="Http Endpoint"/>-->
    <http:inbound-endpoint address="http://esb.local:8090/test.html"/>

        <http:outbound-endpoint
                method="GET" exchange-pattern="request-response"
                address="http://www.weather.com/test.html"
                contentType="text/html" doc:name="HTTP"/>

    <wire-tap>
        <http:outbound-endpoint address="http://logserver/index.php" exchange-pattern="one-way"/>
    </wire-tap>
</flow>

以及如何在控制器端获取这些参数。

我不想在可能的情况下使用“Ajax”或JavaScript

2 个答案:

答案 0 :(得分:2)

首先,您需要通过添加以下代码行来创建自定义路由或在routeconfig文件中启用MapMvcAttributeRoutes。

routes.MapMvcAttributeRoutes();

然后在您定义的操作上方的控制器中添加如下所示的内容。

[Route("/Abc/Details/{id}/{date}")]

如果你想让它可以为空而可以。

[Route("/Abc/Details/{id?}/{date?}")]

您的行动方法如下所示。

[Route("/Abc/Details/{id?}/{date?}")]
public ActionResult Details(int id, string date)

使用@ Html.ActionLink而非硬编码链接。

如果您想使用自定义路线,请将其添加到默认路线

routes.MapRoute(
                "MyCustomRoute",
                "Archive/{entrydate}",
                new { Controller = "ABC", action = "Details",Id  = UrlParameter.Optional,Date =  UrlParameter.Optional});

现在在你看来

@Html.RouteLink("Link Text", "MyCustomRoute", new { Id = YourId, Date=YourDate})

答案 1 :(得分:0)

我发现在Controller Action中声明变量更容易:

public IActionResult Create(string FirstName, string MiddleName, string    
LastName, string ADUsername, string ADId, string ADName, string ADemail)
{
    //do this
}

然后,您可以在视图中按名称分配变量:

<a asp-controller="Employees" asp-action="Create" asp-route-   
FirstName="@item.FirstName" asp-route-MiddleName="@item.MiddleName" asp-
route-LastName="@item.LastName" asp-route-ADUsername="@item.ADUsername" asp-
route-ADId="@item.ADId" asp-route-ADName="@item.ADName" asp-route-
ADemail="@item.ADemail">Add Employee</a>