Create dynamic control in mvc4 razor
i need to create dynamically a textbox with header text when i click add link
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sunny SharmaRank #223 · 11.1K reputationPosted Jul 23, 2013, 4:15 AM EDT
You can do this with jQuery. Follow the pattern below:
$('div.content').append("")
"div.content" is the selector of parent element where you want to append textbox.
Hope it helps.
Cheers!
vidhyaRank #4,868 · 321 reputationPosted Jul 23, 2013, 7:55 PM EDT
i have attached the file for your reference. this was my scenario.
When i click add link, it should add the text box.
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 23, 2013, 9:09 PM EDT
Please refer the below link
http://techbrij.com/add-dynamic-form-elements-textbox-button-radio-checkbox
http://forums.asp.net/t/1763533.aspx/1
Regards,
Iftikar
vidhyaRank #4,868 · 321 reputationPosted Jul 24, 2013, 12:06 AM EDT
When i used like this i am getting this error.
vidhyaRank #4,868 · 321 reputationPosted Jul 24, 2013, 12:27 AM EDT
var totpts = 0;
var bol = 0;
}
@using (Html.BeginForm())
{
}
I have a code which works fine, but i need to display the number before the text box how can i display it.
eg: text1 textbox here
text2 textbox here
text3 texxtbox here
At every click i need to increment the number.
Sunny SharmaRank #223 · 11.1K reputationPosted Jul 24, 2013, 1:00 AM EDT
use this script:
--------------------------------
<script src="~/Scripts/jquery-1.8.2.min.js">script>
<script type="text/javascript">
var textBoxCount = 0;
$(document).ready(function () {
$("#txt").click(function () {
textBoxCount++;
var lbl = $(' + textBoxCount + '').attr({ for: 'text' + textBoxCount });;
var $ctrl = $('').attr({ type: 'text', id: 'text'+textBoxCount, value: 'text' }).addClass("text");
$("#holder").append(lbl);
$("#holder").append($ctrl);
});
});
script>
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend> <span>2.span> Statements & Responseslegend>
<input type="button" id="txt" value="Add TextBox" style="" />
<div id="holder"> div>
fieldset>
div>
}
-----------------------------------------------------------------
It will Work.
Mark it as answer if it helps. Cheers!
vidhyaRank #4,868 · 321 reputationPosted Jul 24, 2013, 1:06 AM EDT
Sunny SharmaRank #223 · 11.1K reputationPosted Jul 24, 2013, 1:13 AM EDT
Obviously, this is why it's dynamic. otherwise you could have set the desired layout in view itself. Contents added dynamically will be reset always. I'm not getting your point on requirements. My Answers satisfies your question. You can ask for more if otherwise needed. Elaborate on what you want to do.
vidhyaRank #4,868 · 321 reputationPosted Jul 24, 2013, 1:19 AM EDT
Did u see my screen which i sent before.
when i click add ,i should add the statement and response text box and label. be stable in the page itself.
Sunny SharmaRank #223 · 11.1K reputationPosted Jul 23, 2013, 9:09 PM EDT
Please don't share only the screen, I'll have to write the whole code for you then. Share the code that you have so far towards the problem and I'll do the modifications for you. It will same my time as well as yours also. Hope you got my point.