0

I have the following code, but it return me an empty FormationDTO object, did I do anything wrong?
I don't understand why it can't properly bind FormationFormViewModel's FormationDTO to the action parameter FormationDTO, it worked in others controllers.

FormationsController

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(FormationDTO formation)
{
    if (!ModelState.IsValid){
        return View("FormationForm", new FormationFormViewModel { FormationDTO = formation, Categories = GetCategories() });
    }
    else{
        // DO THE STUFF
    }

}

FormationForm.cshtml

@model BSS_IT_Education.Models.FormationFormViewModel

@{ 
    ViewBag.Title = "Formation";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Save", "Formations"))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.FormationDTO.Id)

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.FormationDTO.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4">
                @Html.EditorFor(model => model.FormationDTO.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Entrez le nom de la formation..." } })
                @Html.ValidationMessageFor(model => model.FormationDTO.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        // BUNCH OF OTHERS FORM-GROUPS

        <div class="form-group">
            <div class="col-md-offset-2 col-md-8">
                <button type="submit" class="btn btn-success">@((Model.FormationDTO.Id == 0) ? "Sauvegarder  " : "Modifier")</button>                
            </div>
        </div>
    </div>
}

2 Answers 2

1

If I am understanding the code correctly. It looks like you should be passing FormationFormViewModel to the controller action. Not FormationDTO.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer but I don't understand why this is not binding this way. I did the exact same thing on another controller (using viewmodel containing multiple objects and retrieving one of the objects in the POST action) and It worked just nice.
@ArmelMercier When, in a view, you have a form and do model => model.//whatever, it is binding and setting the view's model (whatever is passed into that view). So when you submit the form, you are submitting THAT model to the action.
0

Take a look at the generated HTML on the page. I'm guessing the name attributes on your input elements will look something like formationDTO.name, because your ViewModel is a FormationFormViewModel. But the ModelBinder on the backend is going to look for just a property name, because you are trying to build a FormationDTO.

You may need to manually create those input elements, or use a child action to get the correct ViewModel to a view that lets you use the razor @Html helpers to build the correct elements.

Or, the easier option is to make your controller action accept a FormationFormViewModel, then the ModelBinder should correctly build out the properties of the FormationDTO you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.