This is a migrated thread and some comments may be shown as answers.

Default Values of Model Post as NULL

2 Answers 170 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jako
Top achievements
Rank 1
Jako asked on 18 Sep 2014, 09:06 AM
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 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 18 Sep 2014, 05:34 PM
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?

















0
Jako
Top achievements
Rank 1
answered on 19 Sep 2014, 06:39 AM
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
Tags
General Discussions
Asked by
Jako
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Jako
Top achievements
Rank 1
Share this question
or