简体   繁体   中英

Asp.NET MVC Value of DynamicForm posted but not passed to controller

So what I have is a HTML-Form enabling the user to register for sportsevents. The user can register different profiles (eg his children) and every event can potentially have so called "Additional Attributes" like textboxes for T-Shirt-size etc.

@model Models.EventRegistrationModel
@{
   Layout = null;
   var playerCount = Model.PlayersToRegister.Count;
}
@using (Html.BeginForm("RegisterForEvent", "Event", FormMethod.Post)){
   @Html.AntiForgeryToken()
   <div class="form-group">
    @for (int i = 0; i < playerCount; i++)
    {
        <div>
              <p>@Model.PlayersToRegister[i].User.FullName</p>
        </div>
        <div
              @Html.CheckBoxFor(model => Model.PlayersToRegister[i].PlayerShallGetRegistered)
        </div>
        //this is the "Additional Attributes"-section for each user-profile
        @Html.Raw(Model.PlayersToRegister[i].Form.RenderHtml())
     }
    </div>
 <input type="submit" value="Confirm Registration"/>
}

Since I do not create those events, I cannot know, what these "Additional Attributes" look like, which is why they are rendered dynamically using DynamicForm. My problem is that I cannot access the user-input for those attributes in the controller. When I check the browser's console, I see the input being posted, but checking the dynamic form's value, it always says "null". Here's my controller:

[HttpPost, ValidateAntiForgeryToken]
    public ActionResult RegisterForEvent(EventRegistrationModel model)
    {
        for (int i = 0; i < playerList.Count; i++)
        {
            var form = Session["Form" + i] as Form;
            model.PlayersToRegister[i].Form = form;
            //var test = form
            //var testtest = test.GetResponses(false);
        }
        return RedirectToAction("EventOverview");
    }

As you can see, I tried to use the Form's "GetResponses"-Method, but it returned null.

public List<Response> GetResponses(bool completedOnly)
    {
        return InputFields.OrderBy(inputField => inputField.DisplayOrder).Select(field => new Response
        {
            Title = field.Title, Value = field.Response
        }).Where(response => !completedOnly || !String.IsNullOrEmpty(response.Value)).ToList();
    }

At the moment I am trying to get the values via Session, as this worked in an older version, where you were only able to register one profile at a time. The Session-variable gets assigned in the ActionResult returning the above View.

I've been trying various solutions from various threads over the past days (eg ModelState.Clear), but unfortunately nothing has been successful.

If you need more information, code or whatever, please let me know.

Since your form is dynamic you may want to use a dynamic model in the post method. Try something like this:

[HttpPost, ValidateAntiForgeryToken]
public ActionResult RegisterForEvent(FormCollection collection)
{
    // stuff....
}

You'll have to do a bit of work parsing out the collection that comes in but it should have everything that was posted from the form. In general I don't recommend this as it can really spiral out of control. Having a well defined view model (as you did in the original posting) is much better in general. However, sometimes you really need something dynamic and this gets the job done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM