1

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" />

    }

  }
  }
Dave
  • 263
  • 5
  • 20
  • Why do you think you need a separate `Count` property? Just use `for (int i = 0; i < Model.Books.Count; i++)` - your collection is `List` which has a `Count` property –  Jul 10 '16 at 04:32
  • Even I tried this before but how the List get the count as it will be decieded at run time the number of times the user will click the button then I will get the count. – Dave Jul 10 '16 at 04:36
  • You need to add a new `Book` to your collection each time (otherwise nothing will be bound correctly). But you approach is poor from a performance point of view. Refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/29837547/set-class-validation-for-dynamic-textbox-in-a-table/29838689#29838689) –  Jul 10 '16 at 04:39
  • So your suggesting Jquery would be the correct way to deal with this situation. – Dave Jul 10 '16 at 04:44
  • It would certainly give you better performance rather that hitting the controller each time. But if you do want to do that, then you need `[HttpPost] public ActionResult Add(BookViewModel model) { model.Books.Add(new Book()); return View(model); }` to add the new `Book` and display it in the view. But that seems kine of pointless. You may as well have a separate view to add one book at a time. –  Jul 10 '16 at 04:46
  • Performance is not a concern as this page will be used by admin only one user,i tried ur code public ActionResult Add(BookViewModel model) { model.Books.Add(new Book()); return View(model); } Error object reference not set to an instance of an object. – Dave Jul 10 '16 at 04:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116909/discussion-between-stephen-muecke-and-dave). –  Jul 10 '16 at 04:54

0 Answers0