简体   繁体   English

ajax.beginform()不更新局部视图mvc

[英]ajax.beginform() not updating partial view mvc

I am trying to do something like updatepanel in asp.net. 我正在尝试在asp.net中执行诸如updatepanel之类的操作。 Page A display stuff and a div that partial render another view (Page B) with 2 buttons in it. 页面A的显示内容和一个div局部渲染了另一个具有2个按钮的视图(页面B)。 Depend on which the button, the partial view (Page B) will re-update it content. 依赖于哪个按钮,局部视图(页面B)将重新更新其内容。 The 1st time the page load, the partial view display correct. 第一次页面加载时,局部视图显示正确。 When a button click in the page B, only the view of B is display, Page A is lost. 当按钮单击页面B时,仅显示B的视图,页面A丢失。

Page A:
@{
DemoMVC.Models.FlashADrugModel displayDrug = null;
int drugIndex = 0;

Session["drugIndex"] = drugIndex;

if (Session["userDrugArray"] != null)
{
    DemoMVC.Classes.Drug[] myDrugArray = (DemoMVC.Classes.Drug[])Session["userDrugArray"];

    if(myDrugArray.Length>0)
    {
        displayDrug = new FlashADrugModel { displayDrug = myDrugArray[drugIndex], nullAble = false };
    }
}
}

@using (Ajax.BeginForm("FlashACard", "FlashCard", null, new AjaxOptions() { UpdateTargetId = "updateRegion", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }))
{
    @Html.AntiForgeryToken()
    <div id="updateRegion">
        @Html.Partial("_FlashACard", @displayDrug)
    </div>

    <input type="submit" value="Previous" name="ButtonType"><input type="submit" value="Next" name="ButtonType">
}

.... ....

Page B:    
<dl class="dl-horizontal">
    <dd>
        @Html.DisplayFor(model => model.displayDrug.DrugBrand)
    </dd>
</dl>

..... .....

Controller:
public ActionResult Index()
    {
        //List<Drug> myDrugList =  this.cardRepository.GetAllDrugs();
        List<Drug> myDrugList = new DrugCardsRepository().GetAllDrugs();

        Session["userDrugArray"] = myDrugList.ToArray();

        return View();
    }

    public PartialViewResult FlashACard(FlashADrugModel drugModel, string ButtonType)
    {
        if(!drugModel.nullAble.HasValue)
        {
            if (Session["userDrugArray"] != null)
            {
                Drug[] myDrugArray = (Drug[])Session["userDrugArray"];

                //Default drug index 0
                int drugIndex = 0;

                if (Session["drugIndex"] != null)
                {
                    drugIndex = (int)Session["drugIndex"];
                }

                if (ButtonType == "Next")
                {
                    if(drugIndex < myDrugArray.Length -2)
                    {
                        drugIndex++;
                    }
                }
                if (ButtonType == "Previous")
                {
                    if (drugIndex > 0)
                    {
                        drugIndex--;
                    }
                }

                Session["drugIndex"] = drugIndex;

                drugModel = new FlashADrugModel { displayDrug = myDrugArray[drugIndex], nullAble = false };
            }
        }

        return PartialView("_FlashACard",drugModel);
    }

I found out that adding these lines before the ajax call work. 我发现在ajax调用之前添加这些行。

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

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

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