如何正确地从View发送DropDownList的SelectedValue到控制器?视图模型 [英] How to properly send SelectedValue of DropDownList from View to controller? ViewModel

查看:71
本文介绍了如何正确地从View发送DropDownList的SelectedValue到控制器?视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多解决方案,例如.但是,它不起作用,因为其他示例使用了ViewBag,但是我使用的是ViewModel.

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.

我有ScheduleViewModel:

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

控制器操作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);
    }

并查看:

model CinemaAppl.WebUI.Models.ScheduleViewModel


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

如何通过单击按钮将View的DropDownList的SelectedValue正确地发送到控制器?是否可以在没有AJAX和创建POST方法的情况下发送值?如果不可能,则可以使用AJAXPOST方法.

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.

我想要的是:

我想要DropDownListFor,我只能选择一个可以发送给ActionResult List()DateTime值.

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

我可以看到所有DateTime值:

推荐答案

根据评论,

我想要DropDownListFor,我只能在其中选择一个DateTime值 我可以将其发送到ActionResult List()

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; }
}

现在在您的视野中,

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

在您的HttpPost操作中,

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
}

这篇关于如何正确地从View发送DropDownList的SelectedValue到控制器?视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆