【问题标题】:Attempting to return view model of SelectListItem dropdown [duplicate]尝试返回 SelectListItem 下拉列表的视图模型 [重复]
【发布时间】:2018-11-04 03:14:21
【问题描述】:

这是我的控制器

    [HttpGet]
    public IActionResult Unregister(LinkedServiceTable lst)
    {
        lst.BuildLinkedServices();
        var model = new LinkedServiceTableViewModel
        {
            LinkedServices = GetLinkedServices(lst)
        };
        return View(model);
    }

    [HttpPost]
    public IActionResult Unregister(LinkedServiceTableViewModel vm)
    {
        return View(vm);
    }

    private IEnumerable<SelectListItem> GetLinkedServices(LinkedServiceTable lst)
    {
        var roles = lst.LinkedServices.Select(x => new SelectListItem
        {
            Value = x.LinkedServiceId.ToString(),
            Text = x.ServiceName
        });
        return new SelectList(roles, "Value", "Text");
    }

这是我的观点

@model CDIWeb.ViewModels.LinkedServiceTableViewModel
@{
    ViewData["Title"] = "Unregister Linked Service";
}

<h2>@ViewData["Title"]</h2>

<form action="/LinkedService/Unregister" method="post">
    @Html.LabelFor(m => m.SelectedLinkedServiceId)
    @Html.DropDownListFor(m => m.SelectedLinkedServiceId, Model.LinkedServices)
    <button type="submit" value="Submit">Submit</button>
</form>

我要做的就是提交 myrazor 视图,然后在选中下拉列表中的已提交选项的情况下重定向回同一页面。但是,我在 HttpPost 视图上收到此错误,HttpGet 视图工作正常,我能够成功提交。

InvalidOperationException:具有键“SelectedLinkedServiceId”的 ViewData 项属于“System.Int32”类型,但必须属于“IEnumerable”类型。

关于为什么会发生这种情况以及如何解决它的任何想法?

【问题讨论】:

  • 我认为错误意味着Model.LinkedServices 为空,因此它认为您的第一个参数是选择项列表。
  • @TravisJ 抱歉,为了进一步澄清,我的 HttpGet 视图很好,我得到了正确的下拉列表,但是当我单击提交并重定向到 HttpPost 视图时,我得到了那个错误。
  • 是的,很可能是因为LinkedServiceTableViewModel 不包含实例化的LinkedServices 列表(如,它为空)。
  • 如果LinkedServices在HttpGet中正确显示,怎么可能为null? LinkedServiceTableViewModel 内置在 HttpGet Unregister Action 中,显示页面,LinkedServices 正确显示在下拉列表中,然后将相同的对象传递给 unregister HttpPost 操作。以后怎么会变成null呢?
  • Get 和 Post 是两个不同的请求。

标签: asp.net asp.net-mvc-4 razor asp.net-core .net-core


【解决方案1】:

问题是您需要再次用数据填充 DropDownList,因为这些数据不会在回发之间持续存在。

[HttpPost]
public IActionResult Unregister(LinkedServiceTableViewModel lst)
{
    // Do something, and display the page again with data.

    lst.BuildLinkedServices();
    lst.LinkedServices = GetLinkedServices(lst);
    return View(lst);
}

【讨论】:

  • 我的帖子有问题,没搞定。获取工作正常。
  • 我更新了答案。您仍然需要重新填写 LinkedServices。
【解决方案2】:

我过去通过执行以下操作解决了这个问题。

创建IEnumerable

@{
     ViewData["Title"] = "Unregister Linked Service";

     IEnumerable<SelectListItem> selectList =
         from cf in Model.LinkedServices
         select new SelectListItem
                    {
                        Selected = cf."your value",
                        Text = cf."your value",
                        Value = cf."your value"
                    };
}

然后,在您创建 IEnumerable 列表后,只需在您的剃须刀中使用它:

@Html.DropDownListFor(m => m.SelectedLinkedServiceId, selectList)

【讨论】:

  • 好的,谢谢,我会试试的。但是我很好奇,这与我现在在控制器级别创建 IEnumerable 有什么不同?
猜你喜欢
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多