Solution 1 :

You do not post the value of machineModels from view to hanlder, try to add below code in your form.

<form enctype="multipart/form-data" method="post">
        @{ int i = 0;}
        @foreach (var x in Model.machineModels)
            <input type="hidden" name="machineModels[@i].Text" value="@x.Text" />
            <input type="hidden" name="machineModels[@i].Value" value="@x.Value" />
     Model: <select name="machineModel" asp-for="machineModel" asp- 
       items="Model.machineModels" required></select>
    <input type="submit" asp-page-handler="Download" value="Download 
  certificate" />


public List<SelectListItem> machineModels { get; set; } = new List<SelectListItem>();

Problem :

I have a simple single page ASP.NET Core Razor app that has a “select” in it that I am populating from a Model variable. The values in the Model variable are cleared by the time that I examine them in the OnPost() method. (The variable named machineModel returns just fine but the variable named machineModels in the Model is cleared by the Post.) What is doing this?

Here’s what the Index.cshtml looks like:

@model IndexModel
   ViewData["Title"] = "Home page";
<div class="text-left">
<form enctype="multipart/form-data" method="post">
   Model: <select name="machineModel" asp-for="machineModel" asp-items="Model.machineModels" required></select>
   <input type="submit" asp-page-handler="Download" value="Download certificate" />

Here’s what the Index.cshtml.cs looks like:

public class IndexModel : PageModel
   public string machineModel { get; set; }
   public List<SelectListItem> machineModels = new List<SelectListItem>();

   // In some other function in the class the machineModels variable is filled (several times) ...

   string modelStr = reader.GetAttribute("value");
   int numMachineModels = machineModels.Count;
   string machineModelIndexStr = numMachineModels.ToString();   
   machineModels.Add(new SelectListItem { Text = modelStr, Value = machineModelIndexStr, Selected = true });

   // And here's the Post method ...

   public IActionResult OnPostDownload()
      // Doesn't matter what's in here, machineModels is already cleared at this point

      return Page();

Thanks in advance for any help you can provide,


Comment posted by Mohammed Sajid

Try to declare machineModeles like a property.

Comment posted by RMW

Thanks Xing but I don’t understand the intent. In my code I use machineModels in a “select” statement. How would the code you propose fill the “select” statement? I assume your code would be in addition to mine. Looks like your code would populate hidden fields – so that they would get posted? machineModels don’t need to be posted – because machineModel will tell me what they selected – they just have to be left alone. Why is machineModels getting cleared? Thanks

Comment posted by RMW

I tried adding your code. I left my “select” statement as it was. machineModels still got cleared – but I did have access to the Texts and Values that were in the hidden fields you had me make. Still seems odd to have to “preserve” them that way when machineModels should have been left alone to start with.

Comment posted by Ryan

@RMW If you do not post

Comment posted by RMW

So everything in the Model that I bind to the View will be cleared by the View prior to reaching the OnPost handler if I don’t post all of the bound Model properties? Even if a property wasn’t bound with a “BindProperty” but was instead only referenced via the “items=xxx” in a “select” statement?

Comment posted by Ryan

@RMW I think you have misunderstood the concept.It is not being cleared but not sending the data in your post request.You could always press F12 to check the request in network tab