Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC Ajax.BeginForm OnComplete pass c# arguments in Razor view

I have the following code in a MVC c# Razor view:

@{string url = "/Projects/_MonthRangesScriptsPartial";}
@using (Ajax.BeginForm(
  "_MonthRanges", 
  "Projects", 
  new { id = ViewBag.InputResourceID }, 
  new AjaxOptions { 
    HttpMethod = "POST", 
    UpdateTargetId = "MonthRanges", 
    InsertionMode = InsertionMode.Replace,
    OnComplete = "updategraphArrayScript(@url,@ViewBag.InputResourceID)"
  }))

The code works almost perfectly, but I would like to resolve the c# variables to their values in the OnComplete line.

That is to say, the Razor engin should resolve the code to (for example):

<form action="/Projects/_MonthRanges/55" 
  data-ajax="true" data-ajax-complete="updategraphArrayScript(/Projects/assets,55)"
  ...>

Rather than:

<form action="/Projects/_MonthRanges/55" 
  data-ajax="true" data-ajax-complete="updategraphArrayScript(@url,@ViewBag.InputResourceID)"
  ...>
like image 580
Chopo87 Avatar asked Oct 27 '25 05:10

Chopo87


1 Answers

Simply create another variable string:

@{
    string url = "/Projects/_MonthRangesScriptsPartial";
    string onComplete = String.Format("updategraphArrayScript({0}, {1})", url, ViewBag.InputResourceID);
}

@using (Ajax.BeginForm(
  "_MonthRanges", 
  "Projects", 
  new { id = ViewBag.InputResourceID }, 
  new AjaxOptions { 
    HttpMethod = "POST", 
    UpdateTargetId = "MonthRanges", 
    InsertionMode = InsertionMode.Replace,
    OnComplete = @onComplete
  }))
like image 190
Samuel Caillerie Avatar answered Oct 29 '25 21:10

Samuel Caillerie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!