简体   繁体   English

Html.ActionLink文本框值

[英]Html.ActionLink Textbox Value

I need to put this value: 我需要把这个值:

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

          })

into here: 进入这里:

@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"] is null and I cannot get the value, when I click the link. 单击链接时,ViewData [“ region”]为null,无法获取该值。 How do I put the DropDownList's region value into the query string? 如何将DropDownList的区域值放入查询字符串? I set the region to a default of MA in my code: 我在代码中将区域设置为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";
         }
    }

Since you can't set the URL on page load, I'd just add a click listener to the anchor tag, and update the link there: 由于您无法在页面加载时设置URL,因此我只需将一个点击侦听器添加到锚标记,然后在其中更新链接:

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

The handler should retrieve the region from the regions drop-down, and update the link's href : 处理程序应从region下拉列表中检索该区域,并更新链接的href

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

When user see your page it has already been rendered with link values, and i guess you want the selected value to be placed in that link after user made her choice. 当用户看到您的页面时,它已经使用链接值进行了渲染,我想您希望在用户做出选择后将所选值放置在该链接中。 Client-side thing. 客户端的东西。 From your code - you pass to the controller a value from ViewData["region"] which isn't populated on the controller from the first place. 从您的代码中-您将一个ViewData [“ region”]的值传递给控制器​​,该值从一开始就没有填充在控制器上。 That's why you get null. 这就是为什么您会得到null的原因。 Use ajax to populate viewdata on selection changed. 使用ajax填充选择更改后的视图数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM