6

I need to pass which button is clicked for the form submit. But the hidden input is not received in the controller. Below are my code snippets

View Model:

public class DocumentViewModel
{
    public int Id { get; set; }

    public int ActionId { get; set; }
}

razor(cshtml):

<form....>
    <input id="docActionId" name="docActionId" asp-for="ActionId" type="hidden" value="initialValue" />
</form>

JavaScript:

$("#save_btn").on("click", function ()
{
    $("#docActionId").val("test1");
});

$("#submit_btn").on("click", function ()
{
    $("#docActionId").val("test2");
});

I tried showing the current value in alert function if it is change and it did.

In my Controller/Action when I debug, I get a null value for the ActionId variable. I think this is very simple but I don't know what I did wrong.

Please help. I'm stucked in this for 2 hours now. Thanks in advance.

3
  • 2
    can you please post the controller code that is accepting the action Commented Jan 24, 2019 at 10:30
  • Does your controller receive "DocumentViewModel" paramter? Does your action like "public IActionResult Test(DocumentViewModel model)"? Commented Jan 25, 2019 at 5:53
  • Please show the controller action the sends the value to the view. Commented Sep 11, 2020 at 18:28

4 Answers 4

3

Remove the name="docActionId" html attributes, they will be generated automatically by Razor.

Specifically by the asp-for="ActionId" attribute.

Furthermore the reason this doesn't work is because the name attribute which is docActionId != ActionId which is the name of the property of your object. So it doesn't know where to bind it to.

<form id="myform">
     <input id="docActionId" asp-for="ActionId" type="hidden" value="initialValue" />
     <button type="submit" id="save">Submit</button>
</form>

Try this javascript perhaps the form is submitted before you attach the value

var saveButton = documnet.getElementById('save')
saveButton.addEventListener('click', function(e) {
   e.stopPropagation();
   documnet.getElementById('docActionId').value = "test value"
   documnet.getElementById('myform').submit()
})
Sign up to request clarification or add additional context in comments.

1 Comment

I tried both of what you said. I change the name and id to "ActionId", I removed the name attribute, neither worked. Still getting null value.
2

remove the "Id" attribute... that's all you need. "asp-for" does that. adding yours duplicates it kind

Comments

0

I thing you should use bind property like this:

<form id="myform">
     <input id="ActionId" asp-for="ActionId" type="hidden" value="initialValue" />
     <input id="Id " asp-for="Id " />
     <button type="submit" id="save">Submit</button>
</form>
[BindProperty]
public class DocumentViewModel
{
    public int Id { get; set; }

    public int ActionId { get; set; }
}

Comments

-2
<input type="hidden" id="tag" >

type="hidden" didn't work for me with .net core 7 but only hidden keyword worked.

<input hidden id="tag">

Comments

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.