0

I'm writing an application in asp.net core 2.0.

I have some data that I send to the controller from the view but I also have data that I want to pass to the same controller but they are not in the form.

It is possible to pass this data to the same controller and I do not have to create new inputs.

How to pass data to the controller from outside the form or in form but without creating new inputs.

I have two variables @Model.MinUppercase ,@Model.MinLowercase but I do not use them in the form, how can I pass them to the controller together with variables from the form?

@model Project2.Models.UserModel

<h1>Step2: Register</h1>
<div>
    @if (@ViewData["Message"] != null)
    {
        <div class="alert alert-danger space text-center">
            @ViewData["Message"]
        </div>
    }

@Model.MinUppercase
@Model.MinLowercase


    <center>
        <h2>Register form:</h2>
    </center>
    <div>

        <form asp-action="Middle" asp-controller="Home" method="POST" class="form-wrapper">
            <div class="input-group">
                <label>Login:</label>
                <input id="Login" asp-for="Login" type="text" class="input" size="35">
            </div>
            <div class="input-group">
                <label>Password:</label>
                <input id="Password" asp-for="Password" type="Password" class="input" size="35">
            </div>
            <div class="input-group">
                <label>Your Regex Description:</label>
                <input id="Description" asp-for="Description" type="text" class="input" size="35" value=@Model.Description>
            </div>
            <div class="input-group">
                <label>Your Regex:</label>
                <input id="Reg" asp-for="Reg" type="text" class="input" size="35" value=@Model.Reg>
            </div>
            <div class="input-group">

                <a asp-controller="Home" asp-action="CreateRegex">
                    <button type="button" class="btn btn-danger">Back</button>
                </a>
                    <button class="btn btn-success">Create</button>
            </div>
        </form>

    </div>
Lukasz_K_K
  • 387
  • 3
  • 13

2 Answers2

1

Inside your form html content add hidden fields (using razor helper @Html.Hidden/For):

@Html.Hidden("MyName", "Carlos")

or

@Html.HiddenFor(i => i.PropertyOfYourModel) - if is some property of your model.

solrac
  • 629
  • 3
  • 16
0

As carlosfcmendes already answered you can add data with hidden fields. Or if you want to be more flexible you can intercept the form submit and add data. There are plenty of answers here on how to do that:

Intercept form, add data, ajax, then submit.

How can I listen to the form submit event in javascript?

Vanice
  • 676
  • 5
  • 15