Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my first application on mvc and haven't much idea of mvc 4 Razor
i want to simply insert data from one page in database including 2 dropdowns First for city and second for status i have three tables first on which data is to be inserted and rest two for city and status what i am trying to do is bind dropdown from city and status table and save its id in main table i have created model for it
C#
 public class UserCallDetailModel
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [DataType(DataType.Text)]
        [Display(Name = "Name")]
        public string Name { get; set; }
        [Required]
        [StringLength(500, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
        [DataType(DataType.MultilineText)]
        [Display(Name = "Address")]
        public string Address { get; set; }
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 10)]
        [DataType(DataType.PhoneNumber)]
        public string ContactNo { get; set; }
        [DataType(DataType.Text)]
        public string EmailID { get; set; }
        [DataType(DataType.Text)]
        public string ProductCat { get; set; }
        [DataType(DataType.Text)]
        public string OrgName { get; set; }
        [DataType(DataType.Text)]
        public string WebSite { get; set; }
        [DataType(DataType.Text)]
        public string FollowUpDetail { get; set; }
        [DataType(DataType.Date)]
        public DateTime FollowUpDate { get; set; }
        [DataType(DataType.Url)]
        public string VisitedSite { get; set; }

        public IEnumerable<SelectListItem> Employees { get; set; }
        [DataType(DataType.Currency)]
        public decimal FinalAmount { get; set; }
        [DataType(DataType.Date)]
        public DateTime LastDate { get; set; }

        public decimal CreatedBy { get; set; }

        public DateTime CreatedDate { get; set; }
        public decimal ModifyBy { get; set; }
        public DateTime ModifyDate { get; set; }
        public bool Enable { get; set; }
        public virtual ICollection<CustomerStatus> StatusID { get; set; }
        public virtual ICollection<City> CityID { get; set; }
    }

    public class CustomerStatus
    {
        public decimal StatusID { get; set; }
        public string StatusName { get; set; }
    }
    public class City
    {
        public decimal CityID { get; set; }
        public string CityName { get; set; }
    }

and controller like using (var db = new TenderKhabarCRMModels())
             {
                SelectList selectList = new SelectList(db.City, "CityID", "CityName");
                ViewBag.EmployeeList = selectList;

                 db.UserCallDetailModel.Add(model);
                 db.SaveChanges();
             }

             return RedirectToAction("UserCallDetailList");
but nothing is happening can some one help 
Posted

Create a class Dropdownlist like below

Try like this
C#
using System.Collections.Generic;using System.Web.Mvc;

namespace EDaaS.Domain.Utilities
{
    public static class DropDownList<T>
    {
        public static SelectList LoadItems(IList<T> collection, string value, string text)
        {
            return new SelectList(collection, value, text);
        }
    }

}

Your View
C#
@Html.DropDownListFor(model => model.CityID, (IEnumerable<SelectListItem>)ViewBag.EmployeeList, "--Select--", new { @class = "select" })


Your ActionResult
C#
ViewBag.EmployeeList =
                Domain.Utilities.DropDownList<City>.LoadItems(db.City.ToList(), "CityID", "CityName");

Hope this helps
 
Share this answer
 
v3
Comments
Vishal Pand3y 3-Sep-13 5:19am    
i have done it but still unable to get dropdown in view :(
Jameel VM 3-Sep-13 5:47am    
please post the mark up also
Vishal Pand3y 3-Sep-13 5:48am    
public ActionResult UserCallDetail()
{

return View();
}

[HttpPost]
[AllowAnonymous]
public ActionResult UserCallDetail(UserCallDetailModel model)
{
var db = new TenderKhabarCRMModels();
var query = db.City.Select(c => new { c.CityID, c.CityName });
ViewBag.CityID = new SelectList(query.AsEnumerable(), "CityID", "CityName", 3);
return View();
}
Vishal Pand3y 3-Sep-13 5:49am    
as this is my my first day in mvc so i haven't much idea
Jameel VM 3-Sep-13 6:31am    
i mean the view html mark up code
Please on page render populate the list as ViewBag.List= ......

@Html.DropDownListFor(model => model.Id, new SelectList(ViewBag.List, "value", "text", Model.SelectedId))
 
Share this answer
 
Comments
Vishal Pand3y 3-Sep-13 8:14am    
i have something like
public ActionResult UserCallDetail(UserCallDetailModel model)
{
var db = new TenderKhabarCRMModels();
ViewBag.List =
TenderKhabarCRM.Utility.DropDownList<city>.LoadItems(db.City.ToList(), "CityID", "CityName");
return View();
} and getting this error ........
Vishal Pand3y 3-Sep-13 8:15am    
and these are the namespace i have added using System.Collections.ObjectModel;
using System.Web.Mvc;
using TenderKhabarCRM.Models;
using System.Collections.Generic;
using System.Linq;
Vishal Pand3y 3-Sep-13 8:18am    
namespace TenderKhabarCRM.Models
{
public class TenderKhabarCRMModels : DbContext
{
public DbSet UserCallDetailModel { get; set; }
public DbSet City { get; set; }
public DbSet CustomerStatus { get; set; }
}
}

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