I'll admit I'm new to MVC and this question might be a single case of RTFM. But I'm googling this problem and I can't seem to find a solution.

I've got a simple view used to fill out some details for a specific model. I need to render part of the form using Html.Partial (in truth this is a wrapper which renders old non-MVC controls used from another project).

I've no problems getting data FROM the controller INTO the view.

So what's the issue? How do I get user input from the partial view back to the controller after the user pressed the submit button?


Here's the view and controller I've currently got:

@model Poll

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

    <div class="form-horizontal">
        <h4>Poll</h4>
        <hr />
        @*@Html.ValidationSummary(true)*@
        @Html.HiddenFor(m => m.Id)
        @Html.HiddenFor(m => m.Name)
        @Html.Partial("~/ControlPlaceholder/QuestionPlaceholder.ascx", Model, new ViewDataDictionary(Model))

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Fill" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

The view has been copied almost one-to-one from the standard generated edit view available in MVC5. Note that this is currently just a PoC - normally the whole thing should render a QuestionPlaceholder for every question in a Poll.

Here's the relevant part of the controller:

    //
    // GET: /Poll/Fill
    [HttpGet]
    public ActionResult Fill(Guid id)
    {
        var poll = pollRepository.Get(id);
        return View(poll);
    }

    //
    // POST: /Poll/Fill
    [HttpPost]
    public ActionResult Fill(Poll poll, FormCollection collection)
    {
        try
        {
            return RedirectToAction("Index");
        }
        catch
        {
            return View(poll);
        }
    }

Related posts

Recent Viewed