Attaching a hidden text box to an MVC form

This may very well turn out to be a very stupid question, but basically I have this “form” in the model, which is tied to my view as a form, but I was not able to actually transfer any data from the view. It has only two properties: the Id property and the String property. I tried to populate the String property with text from a hidden text field on the page with no luck.

Form Code:

public class AllocateListForm { public int Id { get; set; } public virtual string HiddenText { get; set; } } 

Corresponding view code:

  <% using (Html.BeginForm("SaveExit", "User", new { }, FormMethod.Post, new { id = "selectExitPoints" })) { %> <fieldset> <input type="hidden" id="HiddenText" /> </fieldset> <% } %> 

There is jQuery behind the scenes that fills the HiddenText text with text, and I can assure you that it is being filled. There is also jQuery behind the scenes that does the Ajax presentation, and I can promise you that this code works because it is used elsewhere in the application without any problems. When I perform an action that sends the form to the server, and I go to my controller code that it points to, I have a breakpoint, so I can go to the console and check if there is any data in the HiddenText field on the form zero. Can someone point me in the right direction?

+6
source share
2 answers

If you assign the login name "HiddenText", then the connecting device must pick it up. I assume that your controller action accepts AllocateListForm as a parameter.

 <input type="hidden" name="HiddenText" id="HiddenText" /> 

You can also use Html Helpers like this:

 @Html.HiddenFor(model => model.HiddenText, new { id = "HiddenText" }) 

EDIT: Add an AllocateListForm as a property of your main model, and then change the @Html.HiddenFor(model => model.MyAllocateListForm.HiddenText)

+8
source

This should do the trick if you want to do it Razor-way.

 @Html.HiddenFor(model => model.HiddenText); 
+4
source

Source: https://habr.com/ru/post/946181/


All Articles