I want to add text box based on the number of times the user has clicked the button if user has clicked the button 5 times then 5 text box should be added,after researching I any how managed to add text box dynamically with unique Ids but am stuck to retain the count value on second,third and so on hits.
I wanna do it through C#
Model
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public DateTime DatePublished { get; set; }
}
public class BookViewModel
{
public List<Book> Books { get; set; }
public int Count { get; set; }
}
Action
public ActionResult Add()
{
return View();
}
public ActionResult Submit(BookViewModel book)
{
//Here I am setting the count to 0 thats why again the counting starts on second hit but on first hit its required,very confused.
int Count = 0;
book.Count = ++Count;
return View(book);
}
Add View
@{
using (Html.BeginForm("Submit", "Dynamic"))
{
<input type="submit" value="Submit" />
}
}
Submit View
@model MVCLearning.Models.BookViewModel
@{
using (Html.BeginForm("Submit", "Dynamic"))
{
if (Model.Count > 0 && Model != null)
{
for (int i = 0; i < Model.Count; i++)
{
<div id="div_@i">
@Html.TextBoxFor(m => Model.Books[i].Title)
</div>
}
<input type="submit" value="Submit" />
}
}
}