Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I´m new in asp.net, and I'm using EF, and I want to implement BeginCollectionItem into my application, so in my model I have DomainEmails,DomainPasswords and DomainUsers can be added dynamically when I creating a new WebPage using somthing like "Add new" and ask me for another DomainEmails,DomainPasswords and DomainUsers

There is my model:

C#
 public class WebPages
    {
        [Key]
        public int WebPagesId { get; set; }
        public int ClientsId { get; set; }
        [ForeignKey("ClientsId")]
        public virtual Clients Clients { get; set;}
        public String ClientsName { get; set; }
        public String DomainName { get; set; }
        public String DomainEmails { get; set; }
        public String DomainUsers { get; set; }
        public String DomainPasswords { get; set; }
          
    }
}


Viewmodel:
C#
public class WebPagesViewModel
   {
       public String DomainName { get; set; }
       public String DomainEmails { get; set; }
       public String DomainUsers { get; set; }
       public String DomainPasswords { get; set; }
       public int SelectedClient { get; set; }
       public IEnumerable<SelectListItem> Clients { get; set; }
   }


Get Controller:

C#
public ActionResult Create(WebPages model)
       {

           var vm = new WebPagesViewModel
           {
              
               Clients = new SelectList(db.ClientsList, "ClientsId", "ClientsName"),

           };


           return View(vm);
       }


Post Controller:

C#
[HttpPost]
        [ValidateAntiForgeryToken]
        
        public async Task<ActionResult> Create([Bind(Include = "DomainName,DomainEmails,DomainUsers,DomainPasswords,ClientsName,SelectedClient")] WebPagesViewModel model)
        {
         

            model.Clients = new SelectList(db.ClientsList, "ClientsId", "ClientsName");
            try
            {
                if (ModelState.IsValid)
                {
                    model.WebPagesCreate();
                   
                    return RedirectToAction("Index", "WebPages");
                }

            }
            catch (Exception)
            {
              //Exception code
            }

            return View(model);
        }


Thankyou in advance!

What I have tried:

I search how to do that but examples are to different than I want to do, If any one can help me I really appreciate it!
Posted
Updated 24-Feb-16 14:03pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900