【问题标题】:Html.ActionLink Textbox ValueHtml.ActionLink 文本框值
【发布时间】:2012-11-15 02:09:52
【问题描述】:

我需要输入这个值:

  @Html.DropDownList("region", new[]{ 
           new SelectListItem() { Text = "MA", Value = "MA" },
                new SelectListItem() { Text = "BC", Value = "BC" },
                     new SelectListItem() { Text = "ON", Value = "ON" },

          })

到这里:

@Html.ActionLink("<<", "List", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, zipCode = ViewData["zipCode"], searchType = ViewData["searchType"], distance = ViewData["distance"], region = ViewData["region"], country = ViewData["country"] })

ViewData["region"] 为空,当我单击链接时无法获取该值。如何将 DropDownList 的区域值放入查询字符串?我在代码中将区域设置为默认的 MA:

public ViewResult List(string sortOrder, string currentFilter, string searchString, int? page, string zipCode, string region, int? distance, string searchType, string country)
    {
        _storeService = new StoreService();

         //sets defaults
         if (String.IsNullOrEmpty(zipCode))
         {
             zipCode = "02472";
         }

         if (String.IsNullOrEmpty(region))
         {
             region = "MA";
         }
    }

【问题讨论】:

  • 您是说您希望页面上的操作链接在用户更改选择时更新?还是您只想要页面加载时的“区域”值?

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


【解决方案1】:

由于您无法在页面加载时设置 URL,我只需在锚标记中添加一个点击侦听器,然后在此处更新链接:

@Html.ActionLink("<<", "List", new { ... },
    new { onclick = "updateLink(this);" })

处理程序应该从区域下拉列表中检索区域,并更新链接的href:

function updateLink(anchor) {
    var select = document.getElementById('region');
    var region = select.options[select.selectedIndex].value;
    anchor.href = e.href + "&region=" + region;
}

【讨论】:

    【解决方案2】:

    当用户看到您的页面时,它已经使用链接值呈现,我猜您希望在用户做出选择后将所选值放置在该链接中。客户端的事情。 从您的代码中-您将 ViewData["region"] 中的值传递给控制器​​,该值从一开始就没有填充到控制器上。这就是你得到空值的原因。 使用 ajax 在选择更改时填充视图数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多