我需要这样做select
:
<select class="form-control">
<option>ESPOSO(A)</option>
<option>HIJO(A)</option>
<option>CONYUGUE</option>
<option>ACUDIENTE</option>
</select>
但我不知道如何在 Razor 中做到这一点,我正在查看一些帖子和文章,但它只显示了如何使用数据库中的数据动态加载它们,我只需要静态数据,但我不给如何。
我的Controller
:
// GET: Families/Create
public ActionResult Create()
{
return View();
}
我的View
:
@model OldMutual.Models.Families
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Families</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.documento_identidad, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.documento_identidad, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.documento_identidad, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.parentesco, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.parentesco, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.parentesco, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.names, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.names, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.names, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.fecha_nacimiento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fecha_nacimiento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fecha_nacimiento, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sexo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sexo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.sexo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.celular, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.celular, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.celular, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.telefono, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.telefono, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.telefono, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.codigo_colaborador, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.codigo_colaborador, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.codigo_colaborador, "", 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>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
应该澄清的是,他select
是为他服务的model.parentesco
。
<div class="form-group">
@Html.LabelFor(model => model.parentesco, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.parentesco, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.parentesco, "", new { @class = "text-danger" })
</div>
</div>
您可以执行以下操作以将数据添加到控制器中的模型,
Create
并且下拉值取决于它,但就是这样,例如我把这个模型控制器
看法
首先,我们需要创建与关系关联的模型:
然后,创建一个父类型的静态列表,并通过
ViewBag
生成动态 Html 将其存储在其中:在视图方面,我们只渲染 Html: