2

I've tried many solutuons, for example this, this and this. However, it does not work cause other examples use ViewBag, but I am using ViewModel.

I have ScheduleViewModel:

public class ScheduleViewModel
{
    public int[] SelectedValues { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }       
    public Schedule OneSchedule { get; set; }
}

Controller action List:

    public ActionResult List(ScheduleViewModel scheduleVM)//scheduleVM is always NULL
    {                             
        var model = new ScheduleViewModel();
        IList<SelectListItem> listTime = new List<SelectListItem>();
        DateTime time = DateTime.Today;            
        for (DateTime _time = time; _time < time.AddDays(5); _time = _time.AddDays(1)) //from 16h to 18h hours
        {
            listTime.Add(new SelectListItem() { Value = _time.ToShortDateString(), Text = _time.ToShortDateString() });
        }
        
        model.Values = listTime;
        return View(model);
    }

and View:

model CinemaAppl.WebUI.Models.ScheduleViewModel


@using (Html.BeginForm())
{
    <p>       
        @Html.DropDownListFor(m => m.SelectedValues, Model.Values)
        <input type="submit" value="Filter" />
    </p>
}

How to properly send SelectedValue of DropDownList from View to controller by Button click? Is it possible to send values without AJAX and creating POST method? If it is not possible, it is okay to use AJAX or POST approaches.

What I want is:

I want DropDownListFor where I can choose just one DateTime value which I can send to ActionResult List().

I can see all DateTime values:

enter image description here

Community
  • 1
  • 1
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Yes It is possible to send it without ajax. Did you try without ajax ? Are you getting your problem (posted object is null) when you use ajax code? If yes you should include your relevant code which does the ajax post in the question. – Shyju Apr 02 '16 at 23:01
  • You have an array of selected values so I presume you are wanting `MultiSelectList` not `SelectListItem`. If not, then just get rid of the array and use an int. – Martin Dawson Apr 02 '16 at 23:03
  • @MartinMazzaDawson no, just one selected item from /drop/downList – StepUp Apr 02 '16 at 23:05
  • @Shyju I've tried without AJAX and posted value is always `null`. – StepUp Apr 02 '16 at 23:06
  • @StepUp why are you using an array of ints for selected values then? – Martin Dawson Apr 02 '16 at 23:07
  • `DropDownListFor` binds to a single value, but you property is `int[] SelectedValues` - are you wanting a dropdownlist or a listbox? –  Apr 02 '16 at 23:08
  • @MartinMazzaDawson His code should still work (using `DropDownListFor`) even though SelectedValues property is an array. In the http post action , The view model objects SelectedValues property will be populated with a single item. – Shyju Apr 02 '16 at 23:08
  • And it would never bind anyway since the values in the `SelectList` are `string` (based on a `DateTime`), not `int` –  Apr 02 '16 at 23:09
  • @StephenMuecke I want `DropDownListFor` where I can choose just one `DateTime` value which I can send to `ActionResult List()`. – StepUp Apr 02 '16 at 23:10
  • 1
    Then your property needs to be `string SelectedValues` or (better) `DateTime SelectedValues`) (although I suggest you rename it to say `SelectedDate`) –  Apr 02 '16 at 23:14

1 Answers1

6

As per the comment,

I want DropDownListFor where I can choose just one DateTime value which I can send to ActionResult List()

Since you want to select only one item, you do not need an array. Also change your type (of the property which gets the selected item) to a valid type so that Model binding will be able to map the date (string) value to the property of your view model.

public class ScheduleViewModel
{
    public DateTime? SelectedDate { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }       
    public Schedule OneSchedule { get; set; }
}

Now in your view,

@model ScheduleViewModel
@using(Html.BeginForm())
{
   @Html.DropDownListFor(x => x.SelectedDate, Model.Values)
   <input type="submit" />
}

And in your HttpPost action,

[HttpPost]
public ActionResult List(ScheduleViewModel model)
{
   if(model.SelectedDate!=null)  
   {
     //Safe to access model.SelectedDate.Value now :)
   }
   // to do : Return something
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Sir just wanted to ask you 1 thing like I have 2 buttons back and front and 1 div
    .now when my page load I will make an ajax call and display todays data in to this div.now when user will click on this back button then I will display previous 24 hours data that is of 2nd april data and if user again click on back button then again previous 24 hours data I.e 1st april data and again click then 31st march data like wise.so I just want some guidance from you regarding this.can you please help me
    – I Love Stackoverflow Apr 03 '16 at 07:55