Click here to Skip to main content
15,066,296 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
   
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
Vishal Pand3y 3-Sep-13 6:35am
   
@Html.DropDownList("CityID", (SelectList)ViewBag.CityID, "--Select One--")
Vishal Pand3y 3-Sep-13 6:49am
   
Complete code of view is @model TenderKhabarCRM.Models.UserCallDetailModel
@{
ViewBag.Title = "UserCallDetail";
Layout = "~/Views/Shared/_Layout.cshtml";
}


UserCallDetail


@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

<fieldset>
UserCallDetailModel
<table border="0" cellpadding="8px" cellspacing="0" style="width: 100%">
<tr>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</td>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
</td>
</tr>
<tr>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.ContactNo)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.ContactNo)
@Html.ValidationMessageFor(model => model.ContactNo)
</div>
</td>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.EmailID)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.EmailID)
@Html.ValidationMessageFor(model => model.EmailID)
</div>
</td>
</tr>
<tr>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.ProductCat)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.ProductCat)
@Html.ValidationMessageFor(model => model.ProductCat)
</div>
</td>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.OrgName)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.OrgName)
@Html.ValidationMessageFor(model => model.OrgName)
</div>
</td>
</tr>
<tr>
<td align="right" width="15%">
<div class="editor-label">
@Html.LabelFor(model => model.WebSite)
</div>
</td>
<td width="35%">
<div class="editor-field">
@Html.EditorFor(model => model.WebSite)
@Html.ValidationMessageFor(model => model.WebSite)
Jameel VM 3-Sep-13 7:00am
   
i have updated my answer
Jameel VM 3-Sep-13 7:05am
   
Please make sure that you should also create one property CityID for getting selected city id in the viewmodel
Vishal Pand3y 3-Sep-13 7:14am
   
what is Tolist i got an error Error 1 'System.Data.Entity.DbSet' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Data.Entity.DbSet' could be found (are you missing a using directive or an assembly reference?)
Jameel VM 3-Sep-13 7:21am
   
Add namespace using System.Collections.Generic;
Vishal Pand3y 3-Sep-13 7:23am
   
i have already added this namespace ....
Please on page render populate the list as ViewBag.List= ......

@Html.DropDownListFor(model => model.Id, new SelectList(ViewBag.List, "value", "text", Model.SelectedId))
   
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