skip to Main Content

In an action, can I access different form element value?
For example I have 3 forms, first form contains common element.

<form id="commonSurname">
    <input type="text" id="surname" name="surname" />
</form>
    
<form id="person1" asp-action="getPerson1" method="post">
    <input type="text" id="name1" name="name1" />
    <input type="submit" id="get1" name="get1" value="get1" />
</form>

<form id="person2" asp-action="getPerson2" method="post">
    <input type="text" id="name2" name="name2" />
    <input type="submit" id="get2" name="get2" value="get2" />
</form>

Controller;

public IActionResult getPerson1(string name1, string surname)
{
    //I want use name1 and surname but surname is null
}
    
public IActionResult getPerson2(string name2, string surname)
{
    //I want use name2 and surname but surname is null
}

2

Answers


  1. A hidden field can be included in each form to store the value of surname. This ensures that every form submission sends the surname and you can update th value with js. Here is example for getPerson1 method
    You can try this up, cheers

    document.getElementById('surname').addEventListener('input', function () {
        const surnameValue = this.value;
        document.getElementById('surnameHidden1').value = surnameValue;
    });
    <form id="person1" asp-action="getPerson1" method="post">
        <input type="hidden" id="surnameHidden1" name="surname" />
        <input type="text" id="name1" name="name1" />
        <input type="submit" id="get1" name="get1" value="Submit Form 1" />
    </form>
    Login or Signup to reply.
  2. can I access different form element value?

    To access the form element value, we need the submission form contains the element which name is same with the parameter name and contains the value.

    So, as Harun said, you can add a hidden field in each form to store the value of surname or use JS to add the form data before send request to action method.

    Besides, I think you can also try to use one form to enter the value, then set the asp-action attribute on the submit button, so that you can also submit the form to different action.

    Code like this:

    <form id="commonSurname" method="post">
        <input type="text" id="surname" name="surname" /> 
        <input type="text" id="name" name="name"  />
        <input type="submit" id="get1" value="get1" asp-action="getPerson1" />
        <input type="submit" id="get2" value="get2" asp-action="getPerson2" />
    </form>
    

    Controller:

        [HttpPost]
        public IActionResult getPerson1(string name, string surname)
        {
            //I want use name1 and surname but surname is null
            var name1 = name;
            return Ok($"{name1} - {surname}");
        }
    
        [HttpPost]
        public IActionResult getPerson2(string name, string surname)
        {
            //I want use name2 and surname but surname is null
            var name2 = name;
            return Ok($"{name2} - {surname}");
        }
    

    Then, the result as below:

    click get1:

    button1

    click get2:

    button2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search