Мне сложно показать выпадающий список для представления, значения будут заполнены из базы данных. Я использую MS SQL Server в качестве базы данных.DropDownListFor в MVC 4
Я хочу показать выпадающий список в ApplyingFor, который будет заселен из базы данных.
Пожалуйста, помогите мне. Это модель
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")
}
Я сделал это, но я не могу сохранить данные. – Arka
Какая ошибка? Получаете ли вы выбранное значение в контроллере после отправки? – adiga
измените свойство 'ApplyingFor' в классе' CandidateProfile' на 'int', а затем попробуйте – adiga