0

I have a search object:

public class Search
    {

    [Display(Name = "First name:")]
    [DataType(DataType.Text)]
    public string fname { get; set; }

    [Display(Name = "Surname:")]
    [DataType(DataType.Text)]
    public string sname { get; set; }

    [Display(Name = "Date to (dob):")]
    [DataType(DataType.Date)]
    public DateTime? dateT { get; set; }

    [Display(Name = "Date from (dob):")]
    [DataType(DataType.Date)]
    public DateTime? dateF { get; set; }

    public Search()
    {
        /*
        first_name = string.Empty;
        surname = string.Empty;
        dateT = null;
        dateF = null;
        */            
    }
}

I've instantiated this in the following class:

public class MainModel
{

    public UserML userLM;

    public Search search;

    public MainModel()
    {
        userLM = new UserML();
        search = new Search();           
    }

}

The Main model is what i'm using my view to populate the search object but I keep getting null values on submission (placing break point in my controller)

Example field:

@Html.LabelFor(model => model.search.fname)
@Html.EditorFor(model => model.search.fname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.search.fname, "", new { @class = "text-danger" })

Model passed to view:

WebApp1.MainModel

Is this possible? any ideas?

Seen this also:

create-an-object-inside-another-class-c

rem
  • 85
  • 1
  • 11

2 Answers2

2

You have created Fields in the Model instead of Properties in the MainModel class, you need to expose them as properties by providing get and set so that these can be mutated by ModelBinder of MVC:

public class MainModel
{
    public UserML userLM {get;set;}  // note { get;set; }

    public Search search {get;set;}

     public MainModel()
     {
        userLM = new UserML();
        search = new Search();           
    }

}

This would now enable the Model Binder to properly bind the value back on form post, the model binding wouldn't work on Fields actually.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • This has fixed the issue - as soon as I can accept, i'll do so. Thanks! (Also - don't know why I didn't realize this simple issue!) – rem Aug 08 '17 at 10:50
0

The default value for a string is null. You would need to explicitly state if you want them to be empty strings in the constructor of either your main model like so:

 public MainModel()
 {
    userLM = new UserML();
    search = new Search{
        fname = "", 
        //other properties           
 }

Or by doing this in the constructor of the search object itself.

Alternatively you could use field initializers in the Search class like so:

[Display(Name = "First name:")]
[DataType(DataType.Text)]
public string fname { get; set; } = ""

For the return binding, your fields on the Main model would have to be properties. You can make this so by adding the {get; set;} at the end of each field.

David Watts
  • 2,249
  • 22
  • 33