4

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)"
  ...>

2 Answers 2

4

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
  }))
Sign up to request clarification or add additional context in comments.

4 Comments

Exelent, and so elegant too. Thanks!
Sorry for having to unaccept the solution: As the url is a string, I have to pass it in quote marks string url = "\'/Projects/_MonthRangesScriptsPartial\'"; I can escape the quotemarks so that the String.Format does not replace them with &#39;, but then Ajax.BeginForm decides to replace them with &#39 anyway. Double and tripple escaping don't seam to worth either - any ideas
But if you pass onComplete between quotes as indicated in my code, this should work...
If I pass it between quotes I end up with data-ajax-complete="@onComplete". however I think this is realy a separate question about how razor deals with quote marks, so I will post in a new question
0

Take off the @ when you assign your OnComplete value. You don't need it since you're already in the context of server side code.

@ is used to switch html rendering context to server side code, and <text></text> is used to switch from server side context to html rendering. In example given above, you're already in the server side code context of the BeginForm() method so the @ is not needed. You're not writing the value of onComplete to the page, BeginForm() will handle all that.

1 Comment

Thanks for the info. However, even without the @, I still end up with &#39.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.