我很难在视图中显示dropdownlist,这些值将从数据库中填充。我使用MS SQL Server作为数据库。
我想在正在申请中显示一个下拉列表,该列表将从数据库中填充。
请帮帮我。 这是模型
public class CandidateProfile
{
[Display(Name = "Candidate Name")]
[Required(ErrorMessage = "Provide a Name", AllowEmptyStrings=false)]
[DataType(DataType.Text)]
public string CandidateName { get; set; }
[Required(ErrorMessage = "Provide Your Address",AllowEmptyStrings = false)]
[Display(Name = "Address")]
[DataType(DataType.MultilineText)]
public string Address { get; set; }
[Display(Name = "Phone Number")]
[Required(ErrorMessage = "Provide a Phone Number")]
[RegularExpression("^([07][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 9[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$", ErrorMessage = "Enter Valid Mobile Number")]
public string PhoneNumber { get; set; }
[Display (Name = "Email-Id")]
[Required(ErrorMessage="Provide an email-id")]
[EmailValidator]
public string EmailId { get; set; }
[Display (Name = "Applying For")]
public string **ApplyingFor** { get; set; }
[Display (Name = "Experience")]
[Required(ErrorMessage = "")]
public int Experience { get; set; }
[Display (Name = "Present Location")]
[Required(ErrorMessage = "")]
public string PresentLocation { get; set; }
}
public class ApplyingPositions
{
public IEnumerable<ApplyingPositions> ApplyingPosition { get; set; }
}
public class ApplyingPosition
{
public int APId { get; set; }
public string Designation { get; set; }
}
这是视图:
@model Recruitment.Models.CandidateProfile
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>CandidateProfile</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CandidateName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CandidateName)
@Html.ValidationMessageFor(model => model.CandidateName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneNumber)
@Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailId)
@Html.ValidationMessageFor(model => model.EmailId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ApplyingFor)
<div class="editor-field">
@Html.EditorFor(model => model.ApplyingFor)
@Html.ValidationMessageFor(model => model.ApplyingFor)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Experience)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Experience)
@Html.ValidationMessageFor(model => model.Experience)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PresentLocation)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PresentLocation)
@Html.ValidationMessageFor(model => model.PresentLocation)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:1)
您需要从控制器传递选项列表以进行查看。所以,在返回View之前
var listOfPositions = new List<ApplyingPosition>();
listOfPositions = //Call the service to populate the listOfPositions from Database here
ViewBag.PositionsList = new SelectList(listOfPositions, "APId", "Designation");
在您的视图中而不是@Html.EditorFor(model => model.ApplyingFor)
@Html.DropDownListFor(m => m.ApplyingFor, (SelectList)ViewBag.PositionsList, "Select")
答案 1 :(得分:0)
从您的数据库填充lstApplyingFor(列表)。并在你的控制器
ViewBag.ApplyingFor = new SelectList(lstApplyingFor, "Id", "Name");
并在视图中: -
@Html.DropDownList(m => m.ApplyingFor,"ApplyingFor", "--Select--")
答案 2 :(得分:0)
第一 - 1)您需要为下拉列表创建源 -
var listForApplyingFor= db.Table.Select(o=> new SelectListItem{
Value=o.ID,
Text= o.Text
}).ToList();
ViewBag.ItemsApplyingFor= listForApplyingFor
.Cshtml查看 -
@Html.DropdownListFor(m=>m.ApplyingFor, new SelectList(ViewBag.ItemsApplyingFor,"Value","Text"),"Select...", htmlAttributes:new{ })
如果有帮助,请告诉我。