0

我有一个 ajax actionlink,我将模型和actionattribute作为附加的 html 属性传递

@Ajax.ActionLink("Add action", "SomeAction", "SomeController", Model, new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "SomePartial", HttpMethod = "POST" }, new { actionattribute = "a", @class = "new" })

actionattribute属性正确的值呈现在 html 源代码中。

模型很好地传递给动作,但 html actionattribute参数为空,而不是显示值a

任何想法为什么?我错过了什么?如何让参数工作?

编辑 ***

模型也不传递值。很抱歉造成混乱。最初在 Index 操作中设置的模型值将传递给 SomeAction。否则,SomeAction 不会看到对表单中这些值的任何更新。

这是控制器

    [HttpPost]
    public ActionResult SomeAction(SomeModel model, string actionattribute)
    {
        ViewBag.Msg = "actionattribute: " + actionattribute;
        ViewBag.Msg += "<br />Counter: " + model.Counter;
        ViewBag.Msg += "<br />Code: " + model.Code;
        ViewBag.Msg += "<br />Type: " + model.Type;

        return this.PartialView("_SomePartialView", model);
    }

编辑 2 ***

  1. 我不能使用 Ajax.BeginForm 而不是 Ajax.ActionLink,因为我已经有页面的 Html.BeginForm 并且不能在其中嵌套 ajax 表单。

  2. _SomePartialView 是局部视图,它使用的模型是“主”模型的属性,并通过 _SomePartialView 传递给该局部视图

    @Html.Partial("~/Views/Main/_SomePartialView.cshtml", Model.SomeProperty)

4

2 回答 2

0

您的问题是 htmlattributes 不直接映射到您的操作参数。所以你可能会做这样的事情:

@Ajax.ActionLink("Add action", "SomeAction", "SomeController", new { model = Model, actionattribute = "a" }, new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "SomePartial", HttpMethod = "POST" }, new { @class = "new" })

希望这可以解决您遇到的问题。

于 2015-07-10T15:00:45.163 回答
0

根据您的更新,您需要像这样形成您的操作链接..更新路由值

@Ajax.ActionLink("Add action", "SomeAction", "SomeController", 
    new { Counter = Model.Counter, Code = Model.Code, Type = Model.Type, actionattribute  = "a"}, 
    new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "SomePartial", HttpMethod = "POST" }, 
    new { @class = "new" })
于 2015-07-10T16:02:06.057 回答