I need to do this select
:
<select class="form-control">
<option>ESPOSO(A)</option>
<option>HIJO(A)</option>
<option>CONYUGUE</option>
<option>ACUDIENTE</option>
</select>
But I don't know how to do it in Razor, I was looking at some posts and articles but it only shows how to dynamically load them with data from the database, and I only need that static data, but I don't give how.
My Controller
:
// GET: Families/Create
public ActionResult Create()
{
return View();
}
My 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")
}
It should be clarified that he select
is for him 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>
You could do the following to add data to the model in the controller
Create
and the dropdown values depend on it, but that is the idea, for example I put this modelController
view
First, we need to create the model associated with relationships:
Then, create a static list of the parent type, and store it in it by
ViewBag
generating the dynamic Html:On the view side we only render the Html: