【发布时间】:2020-10-30 04:55:24
【问题描述】:
所以只是想制作一个按钮来调用控制器动作并传递参数......
拥有我认为的一切,但无法在 actionlink 帮助程序中配置/引用参数...
是的,一旦我完成了这个 html 帮助程序设置,我将重构我的按钮 onclick...
<h1 style="font-size:30px">Enter The Core-i Product Key (format RST 102A08R EPCA 00007)</h1>
<form action="/action_page.php">
<label for="productKey">Product Key:</label>
<input type="text" id="productKey" name="productKey"><br><br>
</form>
<p>Click the "Get Key" button and a trial key will be generated custom to your IBMi hardware".</p>
<p>
@Html.ActionLink(
"Get Key",
"GetTrialKey", // controller action
"HomeController", // controller
new { productKey }, // IT DOES NOT LIKE PRODUCTKEY (REFERENCED ABOVE)
new { @class = "btn btn-info" }) // html attributes
</p>
<div class="display-2">
<a button class="text-center, btn btn-info form-control text-white" typeof="button" onclick="location.href='@Url.Action("GetTrialKey(productKey)")'">Get Key</button></a>
<p>
<br />
</p>
</div>
重构为...
查看...
<form action="HomeController/getTrialKey" method="POST">
<label for="productKey">Product Key:</label>
<input type="text" name="productKey" maxlength="22" value="xxx xxxxxxx xxxx xxxxx"><br><br>
<input type="submit" value="Get Trial Key" class="btn btn-primary" />
</form>
控制器...
[HttpPost]
public async Task<IActionResult> getTrialKey(string productKey)
{
当我运行它时,我得到...
找不到此本地主机页面没有找到该网址的网页:https://localhost:44346/HomeController/getTrialKey
【问题讨论】:
-
相反,我认为您应该将按钮设置为提交按钮并将其包含在您的表单中。当用户点击它时,它会提交表单,然后你就有了产品密钥输入值。
-
那么使用 mvc,我将如何使用 productKey 参数调用控制器?
-
将 ProductKey 指定为控制器方法的参数,或者将 ProductKey 作为视图模型的属性之一,并在表单的 action 属性中指定控制器方法的路径。
-
感谢大卫 - 我做到了,并用我现在得到的重构代码和错误更新了我的原始帖子......我相信我快到了。你能看到问题吗?
-
您的错误是由于该操作应该只是 /home/getTrialKey。如果您使用的是 ASP.NET MVC,而不是手动制作表单,请尝试使用 HTML Helpers:
@Using(Html.BeginForm())。这将为您提供一个更好的占位符来放置操作名称、控制器名称、区域名称等。您还可以在视图顶部使用@model为您的视图声明一个模型。这样您就不需要手动制作输入。如果您的视图模型有一个名为 ProductKey 的属性,您可以使用@Html.TextBoxFor(x => x.ProductKey),而不是为输入硬编码名称“productKey”。
标签: html .net asp.net-mvc