简体   繁体   中英

call a c# functions in cshtml when Ajax.BeginForm completed

so have an Ajax.BeginForm that call a PartialView and return a view (which I'm using it for filtering the products)

but I want to change the main view Model too

I mean after the Ajax.BeginForm got completed i want to make ViewBag in the PartiaView action

and after that when Ajax.BeginForm got completed then call a function in cshtml to update the main view model with that ViewBag

how can I do that

at the end this is my goal:

@functions{
public void ChangeModel(){
    Model.Prod = ViewBag.Prod //forget about the casting !
}
}

@using (Ajax.BeginForm("GetStations", "Trains", new { area = "Site" }, new AjaxOptions
{
    OnSuccess = ChangeModel(),
}, null))

OnSuccess is a JavaScript callback function,

the C# code will be

@using (Ajax.BeginForm("GetStations", "Trains", new { area = "Site" }, new AjaxOptions
{
    OnSuccess = 'mySuccessCallback',
}, null))

Your controller will return something like this

return Json(new { Prod = ViewBag.Prod, ... }, JsonRequestBehavior.AllowGet);

the JavaScript will be

function mySuccessCallback(data, status, xhr) {
    var newProdValue = data.Prod; // passed as model from backend

    // ... display the new value anywhere with JS or jQuery
}

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.

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