How to add Dropdownlist in Current View in MVC.
I have below View
@model ERP_App.Models.UserTypeMV
@{
ViewBag.Title = "New User Type";
}
<h2>CreateUserType</h2>
@using (Html.BeginForm("CreateUserType", "User", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(u => u.UserTypeID)
<div class="form-group">
@Html.LabelFor(model => model.UserType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserType, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserType, "", new { @class = "text-danger" })
</div>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(u => u.UserTypeID)
<div class="form-group">
@Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "AllUserTypes")
</div>
controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataBaseLayer;
using ERP_App.Models;
namespace ERP_App.Controllers
{
public class UserController : Controller
{
private BusinessERPDbEntities DB = new BusinessERPDbEntities();
// GET: User
public ActionResult AllUserTypes()
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out usertypeid);
if (usertypeid != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
var List = new List<UserTypeMV>();
var del_ID = 1;
var usertypes = DB.tblUserTypes.Where(x => x.Del_ID != del_ID).ToList();
foreach (var Usertype in usertypes)
{
List.Add(new UserTypeMV() { UserTypeID = Usertype.UserTypeID, UserType = Usertype.UserType, Designation = Usertype.Designation,Del_ID=Usertype.Del_ID });
}
return View(List);
}
public ActionResult CreateUserType()
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out usertypeid);
if (usertypeid != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
var usertypemv = new UserTypeMV();
return View(usertypemv);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateUserType(UserTypeMV usertypemv)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out usertypeid);
if (usertypeid != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
if (ModelState.IsValid)
{
var checkusertype = DB.tblUserTypes.Where(u => u.UserType == usertypemv.UserType.Trim()).FirstOrDefault();
if (checkusertype == null)
{
var newusertype = new tblUserType();
newusertype.UserType = usertypemv.UserType;
newusertype.Designation = usertypemv.Designation;
DB.tblUserTypes.Add(newusertype);
DB.SaveChanges();
return RedirectToAction("AllUserTypes");
}
else
{
ModelState.AddModelError("UserType", "Already Exit!");
}
}
return View(usertypemv);
}
public ActionResult EditUserType(int? usertypeid)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Home");
}
var userid = 0;
var UserTypeID = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out UserTypeID);
if (UserTypeID != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
var editusertype = DB.tblUserTypes.Find(usertypeid);
var usertypemv = new UserTypeMV();
usertypemv.UserTypeID = editusertype.UserTypeID;
usertypemv.UserType = editusertype.UserType;
usertypemv.Designation = editusertype.Designation;
return View(usertypemv);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditUserType(UserTypeMV usertypemv)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out usertypeid);
if (usertypeid != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
if (ModelState.IsValid)
{
var checkusertype = DB.tblUserTypes.Where(u => u.UserType == usertypemv.UserType.Trim() && u.UserTypeID != usertypemv.UserTypeID).FirstOrDefault();
if (checkusertype == null)
{
var editusertype = new tblUserType();
editusertype.UserType = usertypemv.UserType;
editusertype.UserTypeID = usertypemv.UserTypeID;
editusertype.Designation = usertypemv.Designation;
DB.Entry(editusertype).State = System.Data.Entity.EntityState.Modified;
DB.SaveChanges();
return RedirectToAction("AllUserTypes");
}
else
{
ModelState.AddModelError("UserType", "Already Exit!");
}
}
return View(usertypemv);
}
public ActionResult DeleteUserType(int? usertypeid)
{
if
(string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Name");
}
var userid = 0;
var UserTypeID = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out UserTypeID);
if (UserTypeID != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
var deleteusertype = DB.tblUserTypes.Find(usertypeid);
var usertypemv = new UserTypeMV();
usertypemv.UserTypeID = deleteusertype.UserTypeID;
usertypemv.UserType = deleteusertype.UserType;
usertypemv.Designation = deleteusertype.Designation;
usertypemv.Del_ID = deleteusertype.Del_ID;
return View(usertypemv);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteUserType(UserTypeMV usertypemv)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["usertypeid"]), out usertypeid);
if (usertypeid != 1)
{
return RedirectToAction("Admin", "DashBoard");
}
if (ModelState.IsValid)
{
var checkusertype = DB.tblUserTypes.Where(u => u.UserType == usertypemv.UserType.Trim() && u.UserTypeID != usertypemv.UserTypeID).FirstOrDefault();
if (checkusertype == null)
{
var editusertype = new tblUserType();
editusertype.UserType = usertypemv.UserType;
editusertype.UserTypeID = usertypemv.UserTypeID;
editusertype.Designation = usertypemv.Designation;
editusertype.Del_ID = usertypemv.Del_ID;
DB.Entry(editusertype).State = System.Data.Entity.EntityState.Modified;
DB.SaveChanges();
return RedirectToAction("AllUserTypes");
}
else
{
ModelState.AddModelError("UserType", "Already Exit!");
}
}
return View(usertypemv);
}
public ActionResult username()
{
var items = DB.V_User.ToList();
if (items != null)
{
ViewBag.data = items;
}
return View();
}
}
}
i am getting confuse that how to populate dropdown list using above controll and same View(CreateUserType),
i want value in dropdownlist tbl_designation(ID,Designation)