【问题标题】:How to properly send SelectedValue of DropDownList from View to controller? ViewModel如何正确地将 DropDownList 的 SelectedValue 从 View 发送到控制器?视图模型
【发布时间】:2016-04-02 22:41:50
【问题描述】:

我尝试了很多解决方案,例如thisthisthis。但是,它不起作用,因为其他示例使用ViewBag,但我使用的是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>
}

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

我想要的是:

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

我可以看到所有DateTime 值:

【问题讨论】:

  • 是可以不用ajax发送。你试过没有 ajax 吗?当您使用 ajax 代码时,您是否遇到了问题(发布的对象为空)?如果是,您应该在问题中包含您的相关代码,该代码在问题中发布 ajax。
  • 你有一组选定的值,所以我认为你想要的是 MultiSelectList 而不是 SelectListItem。如果没有,那么只需摆脱数组并使用 int。
  • @MartinMazzaDawson 不,仅从 /drop/downList 中选择一项
  • @Shyju 我试过不使用 AJAX 并且发布的值始终是 null
  • 那么你的属性需要string SelectedValues或者(更好)DateTime SelectedValues)(虽然我建议你把它重命名为SelectedDate

标签: c# asp.net asp.net-mvc html.dropdownlistfor


【解决方案1】:

根据评论,

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

由于您只想选择一项,因此不需要数组。还将您的类型(获取所选项目的属性)更改为有效类型,以便模型绑定能够将日期(字符串)值映射到视图模型的属性。 p>

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 操作中,

[HttpPost]
public ActionResult List(ScheduleViewModel model)
{
   if(model.SelectedDate!=null)  
   {
     //Safe to access model.SelectedDate.Value now :)
   }
   // to do : Return something
}

【讨论】:

  • 先生只是想问你一件事,比如我有 2 个前后按钮和 1 个 div
    。现在当我的页面加载时,我将进行 ajax 调用并显示今天数据输入到这个 div.now 当用户点击这个后退按钮时,我将显示前 24 小时数据,即 4 月 2 日的数据,如果用户再次点击后退按钮,则再次点击前 24 小时数据,即 4 月 1 日数据并再次点击然后是 31 月 31 日的数据,比如 wise.so 我只是想从你那里得到一些关于这个的指导。你能帮我吗
猜你喜欢
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多