I am trying to do something like updatepanel in asp.net. Page A display stuff and a div that partial render another view (Page B) with 2 buttons in it. Depend on which the button, the partial view (Page B) will re-update it content. 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.
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.
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.