0

I have a view model 'ScansViewModel' that's being passed as null on a post method on controller. On the GET method it gets passed as a parameter, but if I edit it and try to post a new file, its passed as null.

my classes are as follows

EditApplicantScanViewModel.cs

public class EditApplicantScanViewModel
{   
    public int ApplicantId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public ScansViewModel ScansViewModel { get; set; }           
}

then my view for this

@model Models.ViewModels.EditApplicantScanViewModel

<form asp-action="Edit" method="post" enctype="multipart/form-data">
    <div class="row">
        <div class="col-md-8">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="ApplicantId" />

            <div class="form-group col-md-4">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>

            <div class="form-group col-md-4">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group col-md-4">
                <label asp-for="EmailAddress" class="control-label"></label>
                <input asp-for="EmailAddress" class="form-control" />
                <span asp-validation-for="EmailAddress" class="text-danger"></span>
            </div>

            @Html.Partial("_Scans", Model.ScansViewModel)

            <button type="submit" class="btn btn-primary">
                Save
            </button>
        </div>
    </div>
</form>

In my partial I have the parent model passing in the scan view model.

@model Models.ViewModels.ScansViewModel

<div class="form-row">
    <div class="form-group col-md-4">            
        <input asp-for="File1" >        
    </div>

    <div class="form-group col-md-4">            
        <input asp-for="File2" >        
    </div>

    <div class="form-group col-md-4">            
        <input asp-for="File3" >        
    </div>
</div>  

When I select a new file on the post method its passed as null. Do I have to combine these two models in to one for this to be achieved? as I will need to use the partial in other places too.

hughjazz
  • 45
  • 4
  • The correct approach is to use an `EditorTemplate` (rename your partial to ScansViewModel.cshtml` and put in in the `/Views/Shared/EditorTemplates` folder, then call it using `EditorFor(m => m.ScansViewModel)` –  Aug 07 '18 at 13:16
  • The alternative is to pass the `HtmlFieldPrefix` as per the [getting the values from a nested complex object that is passed to a partial view](https://stackoverflow.com/questions/29808573/getting-the-values-from-a-nested-complex-object-that-is-passed-to-a-partial-view/29809907#29809907) –  Aug 07 '18 at 13:18
  • You can't have public ScansViewModel ScansViewModel { get; set; } names of the type and the variable are the same – Matěj Štágl Aug 07 '18 at 13:20
  • @MatějŠtágl - of course you can. –  Aug 07 '18 at 13:37
  • passing the prefix to the partial using ViewDataDictionary doesn't work as TemplateInfo is only a get. Also html helper didn't work either still being passed as null. so when I added a hidden attribute on the ScansViewModel and passed it to the controller like this `public async Task Edit(int id, EditApplicantScanViewModel editAppVM, ScansViewModel scansVM)` . it worked that way – hughjazz Aug 08 '18 at 13:23

0 Answers0