How to add a hidden field to a form element on the main page

I want to add a hidden field to all views via jquery or javascript. But I want this code to be in MasterPage.Master, so I write the code in one place, and it is added to all the views that I have. Can I do this, if so, how? I am using asp.net mvc 2

+4
source share
2 answers

Your host:

$(function() { $('body').append( $('<input/>') .attr('type', 'hidden') .attr('name', 'foo') .val('some value') ); }); 

or replace $('body') some other selector with a placeholder that you placed somewhere on the main page if you want this hidden field to be inserted at a specific position. You can also insert it into an existing <form> by giving them some identifier or class (if you have more on one page and you want to insert this hidden field into each form).

+18
source

Not sure about MVC, but in the Page_PreRender main page Page_PreRender you can have this code:

 HiddenField field = new HiddenField(); field.ID = "HiddenField1"; field.Value = "SomeValue"; (this.Page.FindControl("form1") as HtmlForm).Controls.Add(field); 

This will result in a hidden form field, regardless of which page uses the main page.

0
source

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


All Articles