c# - MVC - Variable in .cshtml file which updates the html when the controller calls it -


this pretty basic thing guys, still can't figure out. let's have view:

myview.cshtml

<div>     <p>some content here</p>     @myhtmlvariable //calls updatehtml -> myhtmlvariable = s     <p>some more content on here</p> </div> 

mycontroller.cs

public string updatehtml()  {  s = "i'm string"; //changes dynamically, handled different stuff outside  //any string html-conversion needen?  return s; } 

how can "update" variable in view / how have initialize it?

cheers,

ddertyp

use razor's @functions code block declare functions within view way...

@functions {     public string updatehtml()      {         string s = "i'm string";/*or whatever dynamic stuff wanna do*/         return s;     } }  @{     string myhtmlvariable = updatehtml(); } 

then use @myhtmlvariable within code.


Comments