如何在Asp.Net Mvc4中从Enum填充下拉数据库 [英] How Do I Populate Drop Down Data Base From Enum In Asp.Net Mvc4

查看:56
本文介绍了如何在Asp.Net Mvc4中从Enum填充下拉数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个用户注册表单,我想在下拉列表中填写性别值来自enum,而contray和city来自数据库。

制作一个申请在asp.net mvc4。

不要知道如何在一天中填充这个问题

这里是我的代码:

model:

I am making an user registration form in that i want to populate my drop down list in drop down the value for gender come from enum and the contray and city come from data base.
making an application in asp.net mvc4.
dont khow how to populate stuck in this from whole day
here is my code:
model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Medical.Models
{
public class Registration
{
public int User_Id { get; set; }
public int Role_Id { get; set; }
[Required(ErrorMessage = "Please Enter Email Address")]
[Display(Name = "UserName")]
[RegularExpression(".+@.+\\..+", ErrorMessage = "Please Enter Correct Email Address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please Enter Password")]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required(ErrorMessage = "Please Enter Confirm Password")]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string CPassword { get; set; }
public string First_Name { get; set; }
public string Middle_Name { get; set; }
[Required(ErrorMessage = "Please Enter Last Name")]
[Display(Name = "Last Name")]
public string Last_Name { get; set; }
[Required(ErrorMessage = "Please Enter Date Of Birth")]
[Display(Name = "Birth Date")]
public DateTime Dob { get; set; }
[Display(Name="Gender")]
public SelectList gender { get; set; }
[Display(Name = "LandLine1")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Landline1 { get; set; }
[Display(Name = "LandLine2")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Landline2 { get; set; }
[Required(ErrorMessage = "Please Enter Mobile No")]
[Display(Name = "Mobile No")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Mobile1{ get; set; }
[Display(Name = "Mobile No")]
[StringLength(10, ErrorMessage = "The Mobile must contains 10 characters", MinimumLength = 10)]
public string Mobile2{ get; set; }
[Display(Name = "Designitation")]
public string Designitation { get; set; }
}
}



控制器:

--------------


controller:
--------------

using System.Web.Mvc;
using Medical.DataAcessLayer;
using Medical.Models;
using System.Collections.Generic;
namespace Medical.Controllers
{
public class RegistrationController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Register() // Calling when we first hit controller.
{
return View();
}
[HttpPost]
public ActionResult Register(Registration MD) // Calling on http post (on Submit)
{
if (ModelState.IsValid) //checking model is valid or not
{
DataAcessLayer.RegistrationDB objDB2 = new DataAcessLayer.RegistrationDB(); //calling class DBdata
string result = objDB2.InsertData1(MD);// passing Value to DBClass from model
ViewData["result"] = result;
ModelState.Clear(); //clearing model
return View();
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
public System.Collections.IEnumerable theComponentTypes { get; set; }
}
}

查看:

-----------------



View:
-----------------

@model Medical.Models.Registration
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
Register</h2>
@using (Html.BeginForm())
{
<table width="50%">
<tr>
<td>
@Html.Label("Role")
@Html.TextBoxFor(a => a.Role_Id)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Email)
@Html.TextBoxFor(a => a.Email)
@Html.ValidationMessageFor(a => a.Email)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Password)
@Html.TextBoxFor(a => a.Password)
@Html.ValidationMessageFor(a => a.Password)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.CPassword)
@Html.TextBoxFor(a => a.CPassword)
@Html.ValidationMessageFor(a => a.CPassword)
</td>
</tr>
<tr>
<td>
@Html.Label("First Name")
@Html.TextBoxFor(a => a.First_Name)
@Html.ValidationMessage("Enter First Name")
</td>
<td>
</td>
</tr>
<tr>
<td>
@Html.Label("Middle Name")
@Html.TextBoxFor(a => a.Middle_Name)
@Html.ValidationMessage("Enter First Name")
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Last_Name)
@Html.TextBoxFor(a => a.Last_Name)
@Html.ValidationMessageFor(a => a.Last_Name)
</td>
<td>
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Dob)
@Html.TextBoxFor(a => a.Dob)
@Html.ValidationMessageFor(a => a.Dob)
</td>
</tr>
<tr>
<td>
@Html.Label("Gender")
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Landline1)
@Html.TextBoxFor(a => a.Landline1)
@Html.ValidationMessageFor(a => a.Landline1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Landline2)
@Html.TextBoxFor(a => a.Landline2)
@Html.ValidationMessageFor(a => a.Landline2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Mobile1)
@Html.TextBoxFor(a => a.Mobile1)
@Html.ValidationMessageFor(a => a.Mobile1)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Mobile2)
@Html.TextBoxFor(a => a.Mobile2)
@Html.ValidationMessageFor(a => a.Mobile2)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(a => a.Designitation)
@Html.TextBoxFor(a => a.Designitation)
@Html.ValidationMessageFor(a => a.Designitation)
</td>
</tr>
<tr>
<td colspan="2">
<input id="Submit1" type="submit" value="submit" />
</td>
</tr>
</table>
}<code></code>

推荐答案

为什么不只是使用助手吗?在MVC 5中,此处理程序已经可用,最简单的方法是为Enum类型创建编辑器模板。你可以在这里找到帮助者的代码:



http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net- mvc-creating-a-dropdownlist-helper-for-enums.aspx [ ^ ]



祝你好运!
Why not just use a helper for that? In MVC 5 this handler is already available and the easiest way is to create an editor template for Enum types. You can find the code for the helper here:

http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx[^]

Good luck!


这篇关于如何在Asp.Net Mvc4中从Enum填充下拉数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆