How to make a reference between two forms in a same ASP.NET MVC view

Viewed 43

I came from VB and desktop application and I try to do some C# in a MVC project.

I have a view with two forms in it. First form contains combos and textboxes, the second one contains buttons. The combos are getting values from a database via SQL query. When I press the buttons in the second form I want to return the values from the combos in the first form.

In VB, as long as both forms are opened, is very simple, when I click the button is like myVar=Forms![firstFormName]![controlNameIWant] then I can use the variable any way I want.

Now, I have something like that:

<form name="formDown">
@{
    SqlConnection con = new SqlConnection(@ViewBag.ConnectionString);
    SqlCommand cmd = new @SqlCommand();

    con.Open();
    cmd.Connection = con;
}
<h6>Title<span style="padding-left:16%">
        <select name="Title" class="combo">
            <option value="Select">Select...</option>

            @{
                cmd.CommandText = $"SELECT * FROM [dbo].[titles] ORDER BY description";

                dR = cmd.ExecuteReader();
                while (dR.Read())
                {
                    string @theId = $"{dR["idTitle"]}";
                    <option value="@theId">@dR["description"]</option>
                }

                dR.Close();
            }
        </select>
    </span>
</h6>
@{
    con.Close();
}
</form>

and the second form:

<form method="post" name="formUp" style="padding-left:75%">
    <button type="submit" style="position:relative" name="btnAction" value="Update">Update</button>
</form>

In the Controller the code is simple:

    [HttpPost]
    public IActionResult EditEmployee(string? Title, string? btnAction)
    {
          Console.WriteLine($"{Title},{btnAction}");
    }

The Title is always null unless I put the buttons in the same form with combos, which I'm not willing to do. What I want is to get the combo Title value when I press the Update button in a different form in the same View. Is this possible?

Thanks!

0 Answers
Related