Continuing the previous topic ( User add date for List MVC 4 ) I have a new question. You can see in that topic the initial question and the answer that was given to me, but I needed to respond to it but I am not allowed. Following the user's response, I created this code:
public ActionResult Create()
{
ViewBag.EnrollmentId = new SelectList(db.Enrollments, "EnrollmentId", "Name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Component component)
{
if (ModelState.IsValid)
{
db.Components.Add(component);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EnrollmentId = new SelectList(db.Enrollments, "EnrollmentId", "Name", componente.EnrollmentId);
return View(component);
}
public ActionResult CreateByEnrollmentId(int id)
{
var enrollment = db.Enrollments.FirstOrDefault(e => e.EnrollmentId == id);
if (enrollment == null)
return HttpNotFound();
var component = new Component
{
EnrollmentId = enrollment.EnrollmentId
};
return View("Create", component);
}
However when I create a component it actually passes the id (says components / create / 1 for ex.) but continues to give the error "EnrollmentId field is required." Can anyone help me? Thanks
Edit: View Create
@model PFC.Models.Component
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Component</legend>
<div class="editor-field">
@Html.HiddenFor(model => model.EnrollmentId)
@Html.ValidationMessageFor(model => model.EnrollmentId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}