0

我有一个控制器,它使用ajax.actionlink从另一个控制器获取部分视图。我的问题是如何使用该ajax 部分视图的结果来填充我的主要操作中的文本框

这是我正在处理的视图对应于这个视图

@Ajax.ActionLink("Find location", "getzipcode",
new AjaxOptions
{
    UpdateTargetId = "ajax_insert",
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "GET"
});

<div id="ajax_insert">

</div>

@using (Html.BeginForm("create", "service", FormMethod.Post))
{

    // fill Ajax code in this EditFor Result
    @Html.EditorFor(model => model.zipcode)

}

正如您在上面看到的,ajax 只是请求一个名为getzipcode的局部视图,它只是链接到这个动作

public PartialViewResult getzipcode()
{
    var zipco = (from s in db.servicers where s.zipcode == 32839 select s);

    return PartialView("_review", zipco);
}

ajax 工作正常,我只是在将邮政编码插入 EditFor 框中时遇到了困难,我尝试了 @Html.EditorFor(model => model.zipcode,@ajax_insert)但它没有用。

4

1 回答 1

0

您可以使用onSuccess回调函数

    @Ajax.ActionLink("Find location", "getzipcode",
    new AjaxOptions
    {
        UpdateTargetId = "ajax_insert",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET",
        OnSuccess = "fillTextBox"
    });

    <script type="text/javascript>
     function fillTextBox(){
      //Use jquery to fill your text box
     }
    </script>
于 2013-02-25T20:23:02.493 回答