tu_1329 tu_1329 - 1 year ago 86
C# Question

Add items dynamically to a list in razor in .NET 4 using MVC

In my app I have a view with a text box in which the user can input a name one by one and I need to show the list of names next to that text box.

When the app starts, it needs to show that list with other previous names loaded from the database as well, and being able to delete any of those names in that list.

At the end, take that list and save it to my data base... Similar questions has been asked but don't see a simple and straight forward solution for this, some mention javascript for the solution others using controller, but not really sure how to implement this in my view. In my controller, should I receive as a parameter the new value and add it to my model? and then pass it to the view again?

public class Brands
public Name{get;set;}
public List<string> Names{ get; set; }

public ActionResult Index()
var brands = repository.fetchBrands();
var model = new Brands()
Names = new SelectList(brands),
return View(model);

Answer Source

I have a similar issue and I resolved it using JQuery. You can use JQuery to call some function in your controller and then return a partial view. Please take a look here:

.NET use partial view multiple times

and let us know if you still can't find a solution

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download