WebMay 11, 2011 · Is it possible to call a Controller method from an CSHTML (Razor) page? Example: xxxControl.cs: public String Bla (TestModel pModel) { return ... } index.cshtml: @Bla (Model) <-- Error Thanks. Update: Thanks @Nathan. This isn't a good idea to do this on this way. The goal is: I need some formatting string for a field of the Model. WebJun 30, 2011 · If your method doesn't have to return html and has to do something else then you can use a lambda instead of helper method in Razor @ { ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; Func Sum = (a, b) => a + b; } Index @Sum (3,4) Share Improve this answer Follow answered Jun 30, 2011 …
c# - Razor Page - Call method - Stack Overflow
WebJul 26, 2024 · To call a method in the controller from a view you will need to use Ajax call from the view. Have a look at this S.O thread which covers the same topic: Call Method in Controller from View (cshtml) Share Follow answered Jul 26, 2024 at 11:10 benporter 39 5 Add a comment 0 WebThis is how you call an instance method on the Controller: @ { ( (HomeController)this.ViewContext.Controller).Method1 (); } This is how you call a static method in any class. @ { SomeClass.Method (); } This will work assuming the method is public and visible to the view. Share. eemnes high tea
How to define function that returns html in asp.net core
WebMar 24, 2010 · Define "call." This could mean an AJAX call, a link to another page, or posting a form, to name a few possibilities. – 3Dave Mar 23, 2010 at 22:03 Most answers above might already have worked, unfortunately none worked for me. I found here a useful answer from another Stackoverflow post! "; html += " It's nice to meet you! "; return html; } Include the reference and call on any Page... @Html.Raw (Helper1 ("Josh")) Share Improve this answer Follow answered Dec 6, 2024 at 5:49 Josh 11 3 WebTrying to call a controller action method directly from your view is usually a sign of bad design. You have a few options, depending on what you are trying to do: Avoid calling the method, instead put the data in your model, and render the model Use Html.RenderAction Put the method in another class and use normal function syntax. contact monkey add in