【问题标题】:Pass @Html.DropDownList selection using @Html.ActionLink with ViewModel to Controller使用带有 ViewModel 的 @Html.ActionLink 将 @Html.DropDownList 选择传递给控制器
【发布时间】:2015-08-26 02:00:05
【问题描述】:

我已经阅读了两天有关此的帖子,但仍然没有找到答案。我想在我的 ModelView 中捕获 DropDownList 选择,将其传递给 @Html.ActionLink,后者会将其发送回 Controller 中的特定 Action。

我的视图模型:

    public class ViewModelShipments
    {
        public ViewModelShipments()
        {
            tblShipments = new tblShipments();
        }
        public tblShipments tblShipments;
        public IEnumerable<SelectListItem> ShipmentIDsList;
        public string SelectedShipmentID; 
    }

我的控制器:

public ActionResult SelShipment(string SelectedShipmentID)//for ShipmentID change via DropDownList
        {
            int id = db.tblShipments.Max(p => p.ShipmentID); // find last ShipmentID
            if (SelectedShipmentID != null)
            {
                id = Int32.Parse(SelectedShipmentID);//convert text to int
            }

我的观点:

            @Html.DropDownListFor(expression: model=>model.SelectedShipmentID,selectList: Model.ShipmentIDsList) @* render DropDownList object*@
            @Model.SelectedShipmentID @* display value of SelectedShipmentID *@

            <!-- Upon Click, send selected ID to controller --> 
            <!-- the ActionLink is working, but the routeValues: always contain NULL -->
            @Html.ActionLink(linkText: "Submit", actionName: "SelShipment", controllerName: "Shipment", routeValues: Model.SelectedShipmentID, htmlAttributes: null)

为什么 ActionLink(..., routeValues: Model.SelectedShipmentID,...) 总是返回 NULL 给 Controller ? Model.SelectedShipmentID 不会使用 DropDownList 选择的 id 进行更新。请帮忙,因为我的时间不多了。

【问题讨论】:

  • 您需要 javascript/jquery 根据所选值更改链接的 url,或者最好使用表单(可以是FormMethod.Get)发布值。其 null 的原因是 Razor 代码在发送到客户端之前在服务器上进行评估,因此添加的路由值是 Model.SelectedShipmentID 的初始值(它不会因为您在 UI 中选择某些内容而改变
  • 因为ShipmentIDint 类型,所以你的方法参数和模型属性应该是int? SelectedShipmentID(不是string
  • 感谢您的快速回复。我正在研究这些选项。已设法通过将表单发送回控制器来检索 SelectedShipmentID。表单上有 60 个字段,我将在下面查看 @Stephen 的建议,即使用 Ajax 和部分视图来最大程度地减少不必要的数据交换。这是使用 ActionLink 的初衷。要学的东西太多了……时间太少了。

标签: asp.net-mvc-5 html.dropdownlistfor html.actionlink


【解决方案1】:

Razor 代码在发送到视图之前在服务器上进行解析,因此您的路由参数的值将是 SelectedShipmentID 的初始值。从下拉列表中选择一个值不会更改您已经呈现的 url。

您可以使用 javascript/jquery 来处理下拉列表的 .change() 事件(或链接 .click() 事件)以更新 url,但是更好的处理方法是使用向控制器发送 GET 的表单方法

@model ViewModelShipments
@using (Html.BeginForm("SelShipment", "Shipment", FormMethod.Get))
{
  @Html.DropDownListFor(m => m.SelectedShipmentID, Model.ShipmentIDsList, "-Please select-")
  <input type="submit" />
}

注意DropDownListFor() 的最后一个参数添加一个标签选项,允许您回发null,但不确定这是否适合您。

由于您绑定到的值是int,那么您的模型属性和方法参数都应该是int? 而不是string。此外,您应该更改控制器中的逻辑,以便在将有效值传递给方法时不会进行不必要的数据库调用。

public ActionResult SelShipment(int? SelectedShipmentID)
{
  if (!SelectedShipmentID.HasValue)
  {
    // only necessary to call database if SelectedShipmentID is null
    SelectedShipmentID = db.tblShipments.Max(p => p.ShipmentID)
  }
  ....
}

旁注:根据您的视图模型属性,我假设您希望根据所选ShipmentID 的值在视图中显示一些数据。如果是这样,您应该考虑使用 ajax 将所选值发布到控制器方法,该方法基于该值返回 tblShipments 数据,作为部分视图或 json,并更新当前页面,而不是进行完整的页面刷新每次。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 2011-02-18
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多