【问题标题】:.net mvc html help ActionLink and parameters to controller class.net mvc html 帮助 ActionLink 和控制器类的参数
【发布时间】: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 =&gt; x.ProductKey),而不是为输入硬编码名称“productKey”。

标签: html .net asp.net-mvc


【解决方案1】:

引用其中一个 cmets:

我并没有阻止您使用 HTML Helpers。我的意思是你构建表单的方式和你使用ActionLink 是错误的。如果您只想将产品密钥发回服务器,那么在表单中输入产品密钥会更容易。

我强烈建议您通读 Microsoft 的文档,至少阅读以下文档:https://dotnet.microsoft.com/apps/aspnet/mvc 以了解 MVC 是什么。从您的代码示例中,我根本没有看到您使用过M - Model


无论如何,如果您只想获取用户输入的产品密钥,我会这样做:


定义控制器

我不喜欢将所有内容都放在 /home 下(即HomeController)的想法。想想对用户有意义的页面的 URL。

现在我猜你想做什么。我看到了产品密钥和试用密钥等术语。一个叫ProductKeyController的控制器怎么样:

public class ProductKeyController : Controller
{
    // This corresponds to /productkeys, and you can list all the product keys
    // on the view it returns.
    public ActionResult Index()
    {
        return View();
    }

    // This corresponds to /productkeys/create, and you can create a specific product
    // key by asking the user to provide a trial key?
    // The view this returns might be the page where you build the form
    public ActionResult Create()
    {
        ...
        return View();
    }

    // This corresponds the form post.
    [HttpPost]
    public ActionResult Create(CreateProductKeyViewModel model)
    {
        ...
        return View(model);
    }
}

视图模型

如果需要,您的 MVC 控制器负责获取数据、构建视图模型并将其传递给视图。当您创建产品密钥时,如果您需要要求用户输入任何内容,您可以在其中声明模型和属性:

public class CreateProductKeyViewModel
{
    [Required]
    [Display(Name = "Trial Key")]
    [MaxLength(22)]
    public string TrialKey { get; set; }
}

视图 Create.cshtml

由于您知道控制器会将视图模型传递给视图,因此您可以在视图顶部声明它,这样您在视图中对视图模型所做的一切都是强类型的。

@model CreateProductViewModel
@{
    Layout = "xxx";
}

<h1>Enter The Core-i Product Key (format RST 102A08R EPCA 00007)</h1>
@using(Html.BeginForm("create", "productKey", new { area = "" }, FormMethod.Post))
{
    @Html.LabelFor(x => x.TrialKey)
    @Html.TextBoxFor(x => x.TrialKey)

    <button type="submit">Create</button>
}

查看视图中的所有内容是如何进行强类型化的?您不必手动创建表单和输入来询问用户试用密钥。

并且当用户输入试用键并按下提交时,它会回发到Create的Post方法。由于视图是使用视图模型声明的,并且视图模型是 create 方法的参数,因此 MVC 已经为您完成了模型绑定,因此您将得到用户在回帖中输入的内容。

这至少可以帮助您入门。

注意:我都是手写的。未测试。

【讨论】:

  • 谢谢!我通过简单的 home/getTrialKey 调整让它工作。效果很好。我已经准备好用于获取密钥的模型和控制器……但感谢您提供的可靠反馈,并确认我知道的比我想象的要多,我只是在过去几周学习 .NET!
猜你喜欢
  • 2016-12-20
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多