1

I am attempting to pass my model on change of a DropDownList in my Razor code but my model information is not getting passed to the post method, even thouugh the post method is being called.

In my Razor:

@model Namespace.Models.MyModel

@using (Html.BeginForm("myFunc", "Home", FormMethod.Post, new { myModel = Model }))
{
    @Html.DropDownListFor(m => m.SelectedType, new SelectList(Model.Types, "Id", "Desc", Model.SelectedType), new { @class = "form-control", style = "display: inline;", @onchange = "this.form.submit()" })
}

My model:

public class MyModel
{
    public List<myData> myList = new List<myData>();
    public List<myType> Types = new List<myType>();
    public string SelectedType;
}

My controller:

[HttpPost]
public ActionResult myFunc(MyModel myModel)
{
    //do stuff
}

If I place a breakpoint in myFunc I can see it stepping into there on change of my DropDownList but the model information does not pass to the function. I'm sure what I am missing is probably a minor thing but I just cannot see it.

The rendered HTML is:

<form action="/Home/myFunc" myModel="Namespace.Models.MyModel" method="post">
   <select class="form-control" id="SelectedType" name="SelectedType" onChange="(this.form).submit()" style="display: inline;">
      <option selected="selected" value="FT">Fault</option>
      <option value="QY">Query</option>
      <option value="RQ">PM Request</option>
   </select>
</form>
10
  • Does your view have a @model MyModel line under your @using statements? Commented Jan 16, 2019 at 16:26
  • myModel not _model Commented Jan 16, 2019 at 16:28
  • You say "the model information does not pass to the function". Do you mean that the SelectedType property is empty or the lists of values are empty? Commented Jan 16, 2019 at 16:28
  • 1
    The view model and the input model are not the same stuff! Prepare proper input model. If you are unsure, check with fiddler or browser tools the exact content of the POST body. Commented Jan 16, 2019 at 16:28
  • Your lists should definitely not be filled on the post, but the SelectedType selected value should. Not clear immediately why it isn't. Commented Jan 16, 2019 at 16:29

1 Answer 1

3

By default it is only looking at public properties not fields.

MVC will try to bind request data to the action parameters by name. MVC will look for values for each parameter using the parameter name and the names of its public settable properties. In the above example, the only action parameter is named id, which MVC binds to the value with the same name in the route values. In addition to route values MVC will bind data from various parts of the request and it does so in a set order.

https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2

public class MyModel
{
    public List<myData> myList = new List<myData>();
    public List<myType> Types = new List<myType>();
    public string SelectedType;
}

I got it to work by changing your model from the top to the bottom one.

public class MyModel
{
    public List<myData> myList {get;set;}
    public List<myType> Types{get;set;}
    public string SelectedType {get;set;}
}
Sign up to request clarification or add additional context in comments.

6 Comments

Might be worth a further explanation that the default is to only serialize/deserialize public properties and not fields.
Will do, Was trying to find a good article detailing it but not finding much. Might just paraphrase real quick.
@RyanSchlueter Thanks Ryan, this did work, I am unsure as to why though
@badNameHere just added some info. Let me know if you need any more explanation man.
Thanks, that is helpful, still relatively new to front end development and I have been stuck on this for ages. Goodluck on making it to 2k rep
|

Your Answer

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