0

so simply want to make a button that will call the controller action passing a parameter...

have everything I believe in place but unable to configure/reference the parameter in the actionlink helper...

Yes I will be refactoring my button onclick once I get through this html helper setup...

        <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>

refactored to...

view...

    <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>

controller...

        [HttpPost]
        public async Task<IActionResult> getTrialKey(string productKey)
        {

when I run it i get...

This localhost page can’t be foundNo webpage was found for the web address: https://localhost:44346/HomeController/getTrialKey

6
  • Instead, I think you should just make the button as the submit button and include that within your form. When the user hits it, it will submit the form and there you have the product key input value. Commented Jul 9, 2020 at 16:29
  • so with mvc, how would I do that to call the controller with the productKey parm? Commented Jul 9, 2020 at 20:17
  • 1
    Specify the ProductKey as the parameter of the controller method, or include ProductKey as one of the properties of your view model, and specify the path to your controller method in your form's action attribute. Commented Jul 9, 2020 at 20:21
  • thanks David - i did that and updated my original post with the refactored code and error I am getting now... i believe I am almost there. can you see the issue? Commented Jul 9, 2020 at 20:49
  • Your error was due to the fact that the action should be just /home/getTrialKey. If you're using ASP.NET MVC, instead of crafting the form manually, try to use HTML Helpers: @Using(Html.BeginForm()). That would give you a better placeholder to put action name, controller name, area name, etc. Also you can declare a model for your view using @model on top of the view. That way you don't need to craft the input manually. Instead of hard coding the name "productKey" for the input, if your view model has a property called ProductKey, you can do @Html.TextBoxFor(x => x.ProductKey). Commented Jul 9, 2020 at 20:57

1 Answer 1

1

Referring back to one of the comments:

I didn't discourage you from using HTML Helpers. I just meant the way you constructed the form and you used ActionLink was wrong. And it would be easier to just have an input for the product key inside the form, if that's the only thing you want to post back to the server.

And I would highly recommend you to read through documentations from Microsoft, at least this one: https://dotnet.microsoft.com/apps/aspnet/mvc to understand what a MVC is. From your code sample, I didn't see you used M - Model at all.


Anyway, if you just only want to get the product key the user types in, I would do it like this:


Define a Controller

I dislike the idea of putting everything under /home (i.e., HomeController). Just think about the URL to the page that would make sense to the user.

Now I am guessing what you are trying to do. I saw terms like product keys and trial keys. What about a controller called 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);
    }
}

The view model

Your MVC controller is responsible to fetch data, if needed, build a view model, and pass it to the view. When you create a product key, if you need to ask the user to enter anything, you can declare a model and properties within it:

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

The View Create.cshtml

Since you know the controller will be passing the view model to the view, you can declare it on top of the view so that everything you do with the view model inside the view is strongly-typed.

@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>
}

See how everything within the view is strongly-typed? You don't have to manually create the form and the input for asking user for the trial key.

And when the user enters the trial key and presses submit, it will post back to the Post method of Create. Since the view is declared with the view model, and the view model is the parameter of the create method, MVC has already done the model binding for you hence you will get what user entered on the post back.

This is at least something to get you started.

Note: I wrote everything by hand. Not tested.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! i got it working with the simple home/getTrialKey tweak. works great. I already have the model and controller in place for obtaining the key... but thanks for that solid feedback with confirm I know more than I thought I did only being into learning .NET for the last couple weeks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.