Default Values of Model Post as NULL

3 posts, 0 answers
  1. Jako
    Jako avatar
    107 posts
    Member since:
    Apr 2012

    Posted 18 Sep 2014 Link to this post

    Hi everyone

    I am pretty new to MVC and Kendo. I have a ViewModel  containing List<> Items I am using to create drop downs etc.

    When I submit the form to the controller, the fields I specify "@(Html.Kendo().DatePickerFor(m => m.ReportCriteria.DateEnd)" etc returns a value via the model.

    But the List<> items etc are passed as empty?

    I would have though that the values begin passed in initially would stay preserved in the model on a post?

    Any advice?

    Thank you.
  2. Kevin
    Kevin avatar
    9 posts
    Member since:
    Aug 2013

    Posted 18 Sep 2014 in reply to Jako Link to this post

    You are passing back a new instance of the ViewModel  on form submit. If you are populating your list of items in the controller then they will be bound to the object you are passing to the view, but not the one you are getting back.

    View Model
        public class ViewModel
        {
            public string End { get; set; }
            public List<string> MyList { get; set; } 
        }

    Controller
    public ActionResult Index()
    {
       return View(new ViewModel() { MyList = new List<string>() { "foo", "bar" } });
    }

    View
    @model TelerikMvcApp1.Models.ViewModel
    @using (Html.BeginForm("FormSubmit", "Home", FormMethod.Post))
    {
        @(Html.Kendo().DropDownListFor(m => m.End).BindTo(Model.MyList))
        <button type="submit">Submit</button>
    }

    Alternatively, if you you constructed the list in the view models constructor you WOULD get the list back on the POST:
    View Model
        public class ViewModel
        {
            public ViewModel()
        {
            MyList = new List<string>() {"foo","bar"};
        }
            public string End { get; set; }
            public List<string> MyList { get; set; } 
        }

    Controller
    public ActionResult Index()
    {
       return View(new ViewModel());
    }

    View
    @model TelerikMvcApp1.Models.ViewModel
    @using (Html.BeginForm("FormSubmit", "Home", FormMethod.Post))
    {
        @(Html.Kendo().DropDownListFor(m => m.End).BindTo(Model.MyList))
        <button type="submit">Submit</button>
    }

    Why do you need the list back on the form submit?

















  3. Jako
    Jako avatar
    107 posts
    Member since:
    Apr 2012

    Posted 19 Sep 2014 in reply to Kevin Link to this post

    Hi Kevin

    Ok great, gives me a better understanding.

    I needed to pass back the Id of the object I am working with to keep track of the process.

    I have created a hiddenfieldfor element and its working great.

    Thanks
Back to Top