Passing id for new MVC 4 controller

2

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")
}
    
asked by anonymous 25.04.2016 / 15:57

1 answer

2

As I said in the previous question, you need to fill in the EnrollmentId as hidden if it has value, or bring the dropdown filled in otherwise. A code hint would be as follows:

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Component</legend>

        @if (Model != null && Model.EnrollmentId != null && Model.EnrollmentId != 0)
        {
            @Html.HiddenFor(model => model.EnrollmentId)
        } 
        else 
        {
            <div class="editor-field">
                @Html.DropDownListFor(model => model.EnrollmentId, (SelectList)ViewBag.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")
}

Make a debug on Action with [HttpPost] to verify that the values are being filled in correctly.

    
25.04.2016 / 23:19