在MVC 3中将routeValues传递给Controller

时间:2011-06-04 01:47:38

标签: asp.net-mvc-3

我在View中有这段代码

    @using (Html.BeginForm())
    {
       @: Location @Html.DropDownList("territryID", (SelectList)ViewBag.Territory,    "choose one")

@Ajax.ActionLink(
    "ok",
    "Info", new { territryID = "#territryID" },
    new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        UpdateTargetId = "post1"
    })

     <div id="post1">
     </div>
    }

和我的控制器中的代码

   [HttpPost]
   public ActionResult Info(int? territryID)
   {
        if (territryID == null)
        {
            return RedirectToAction("Info");
        }

        var model = (from c in _db.OCRDs
                     where c.Territory == territryID
                     select c).Distinct();

        return PartialView("_getCustomerByTerritory", model);
    }

如何将我的dropdownlist选择值传递给控制器​​中的territryID参数,怎么做?

感谢, 埃里克

1 个答案:

答案 0 :(得分:1)

如何做到这一点,将您的URL初始化为类似的内容(还要注意为链接分配id):


@Ajax.ActionLink("ok", "Info", new { territryID = "REPLACEME" }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "post1" }, new { id = "oklink" })

当下拉列表更改时替换占位符(REPLACEME),如下所示:


<script>
$('#territryID').change(function () {
    var newid = $('#territryID').val();
    var oldLink = $('#oklink').attr('href');
    var newLink = oldLink.replace('REPLACEME', newid);
    $('#oklink').attr('href', newLink);
});
</script>