Html.ActionLink文本框值

时间:2012-11-15 02:09:52

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

我需要把这个值:

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

当用户看到您的页面时,它已经使用链接值进行了渲染,我猜您希望在用户做出选择后将所选值放在该链接中。客户端的事情。 从您的代码中 - 您将ViewData [“region”]中的值传递给控制器​​,该值从一开始就没有填充在控制器上。这就是你得到null的原因。 使用ajax在选择更改时填充viewdata。

相关问题