i'm making a user registration page. for to same i'm using dropdown list for gender and role. but when i posting data dropdown values is not passing. here is my code. your timely response will be highly appreciated; here is the image tooenter image description here
//Model class
public class BEUsers
{
public int User_Id { get; set; }
[Required]
public string Name { get; set; }
[DisplayName("Role")]
[Required]
public int? Role_Id { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[DisplayName("Gender")]
[Required]
public int? Gender_Id { get; set; }
public DateTime? CreatedOn { get; set; }
public int? CreatedBy { get; set; }
[DisplayName("Upload")]
public string Imagepath { get; set; }
public DateTime? ModifiedOn { get; set; }
public int ModifiedBy { get; set; }
public HttpPostedFileBase Imagefile { get; set; }
public IEnumerable<BEUsers> ListUsers { get; set; }
public IEnumerable<BERoles> ListRoles { get; set; }
public IEnumerable<BEGender> ListGender { get; set; }
}
//Action methods
[HttpGet]
public ActionResult Create()
{
BEUsers be = new BEUsers();
be.ListRoles = blRole.Get();
be.ListGender = blGender.Get();
ViewBag.Role = new SelectList(blRole.Get(), "Role_Id", "Role_Desc");
ViewBag.Gender = new SelectList(blGender.Get(), "Gender_Id", "Gender_Desc");
return View(be);
}
[HttpPost]
public ActionResult Create(BEUsers be)
{
if (ModelState.IsValid)
{
if (be.Imagefile != null)
{
// save the file
string fileName = Path.GetFileNameWithoutExtension(be.Imagefile.FileName);
string extention = Path.GetExtension(be.Imagefile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extention;
string path = "~/Images/" + fileName;
fileName = Path.Combine(Server.MapPath(path) + fileName);
be.Imagefile.SaveAs(fileName);
be.Imagepath = fileName;
blUser.InsertUser(be);
return RedirectToAction("Index");
}
}
return View();
}
//this is the view
@using (Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Role_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.DropDownList("Role", null, "Select Role",htmlAttributes: new { @class = "form-control" })*@
@Html.ValidationMessageFor(model => model.Role_Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender_Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Gender_Id, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.DropDownList("Gender", null,"Select Gender", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Gender_Id, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.CreatedOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedOn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreatedOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreatedBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedBy, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreatedBy, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.Imagepath, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="Imagefile" id="fileUpload" class="btn btn-default" required />
</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>
}