【问题标题】:redirect action link with changing url in ASP.NET MVC3在 ASP.NET MVC3 中使用更改 url 重定向操作链接
【发布时间】:2012-02-04 06:13:34
【问题描述】:

我想自动回发到下拉列表。 我的表单正在查看:

@using (Html.BeginForm("Index", "Model", FormMethod.Post))
{
            @(Html.Telerik().DropDownList()
                .Name("ddlBrands")
                .BindTo((IEnumerable<SelectListItem>)ViewData["brands"])
                .ClientEvents(events => events
                                  .OnChange("onDropDownListChange")
                )
            )
            <input type="submit" value="OK" />

    <table style="margin:15px; margin-left:0px;">
        @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.FullModel)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id=item.ModelID }) |
                    @Html.ActionLink("Delete", "Delete", new { id=item.ModelID })
                </td>
            </tr>
        }
    </table>
}

和javascript:

<script type="text/javascript">
    function onDropDownListChange(e) {
        SimpleAjaxRequest('/Model/Index', e.value);
    }

    function SimpleAjaxRequest(url, requestData) {
        return $.ajax(
        {
            type: 'POST',
            url: url,
            async: true,
            data: { ddlBrands: requestData },
            dataType: "json",
            traditional: true
        });
    }
</script>

我通过 Ajax 将 Post 数据从 Index 视图发送到服务器,在处理数据后,我需要更新表单上的数据。我该怎么做?我正在尝试重定向到 POST 操作

[HttpPost]
        public ActionResult Index(string ddlBrands)
        {
            SetBrandItems();
            return RedirectToAction("Index", "Model", new {ddlBrands = ddlBrands});
        }

调用 GET 操作后

 public ActionResult Index(int? ddlBrands)
    {
        SetBrandItems();
        List<Model> m = dm.GetModelsByBrandId(ddlBrands).ToList();
        return View(m);
    }

但是我的页面没有刷新,url 没有改变,数据没有更新...... 有人可以帮帮我吗?

【问题讨论】:

  • 我很困惑,您不想刷新页面吗?或者您希望页面刷新?
  • 我想刷新我的页面。但如果你知道其他变体来更新我的表格上的日期,请帮助
  • 你的 GET 索引方法是什么样子的
  • 你能指出你遇到的问题,有什么错误吗?
  • 我没有错误。我的方法正确地接收和发送数据,但是当我重定向调用下一个方法公共 ActionResult 索引(int?ddlBrands)。一分钟 - 我编辑我的帖子

标签: .net asp.net-mvc asp.net-mvc-3 telerik


【解决方案1】:

DropDownList 更改事件是 ajax 操作。我认为你不能在服务器端重定向。但是你可以在 ajax 回调中添加一个 rediect。

例如:

 function SimpleAjaxRequest(url, requestData) {
    return $.ajax(
    {
        type: 'POST',
        url: url,
        async: true,
        data: { ddlBrands: requestData },
        dataType: "json",
        traditional: true,
        success: function() {
            //callback redirect 
            location.href = '/Model/Index';
        }
    });
}

    [HttpPost]
    public ActionResult Index(string ddlBrands)
    {
        SetBrandItems();
        return Json(null, JsonRequestBehavior.AllowGet);
    }

【讨论】:

    【解决方案2】:

    我想自动回发到下拉列表。我的表单正在查看:

    执行标准表单提交:

    function onDropDownListChange(e) {
        $("form").submit();
    }
    

    并返回相同的视图(带有预填充的模型):

    [HttpPost]
    public ActionResult Index(string ddlBrands) {
        SetBrandItems();
        return View("Index", new { ddlBrands = ddlBrands });
    }
    

    或者你想通过 AJAX 实现它?如果是这样,请改用 Ajax.Form:

    @using (Ajax.BeginForm(...)) {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2014-03-07
      • 1970-01-01
      相关资源
      最近更新 更多