Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 8, 2015 2:55 PM
From the view, how do you call a class method? I have a domain model, a void method in the controller. I'm using Razor in the view. Here is the model:
public class MegaMillion
{
public int MegaMillionID { get; set; }
public DateTime Date { get; set; }
public string NumberOne { get; set; }
public string NumberTwo { get; set; }
public string NumberThree { get; set; }
public string NumberFour { get; set; }
public string NumberFive { get; set; }
public string NumberSix { get; set; }
}
Here is the method in the class:
public int Mega_Million1(int a)
{
Random _r = new Random();
// Generates a random number for the For Loop
Random _a = new Random();
int loop = _a.Next(1, 100);
for (int i = 0; i <= loop; i++)
{
a = _r.Next(1, 80);
}
return a;
} // there are six methods that need to be called that are just like the one above.
So I want to call its results in the view, here is my controller:
public void GetMega_Million()
{
gmm.Mega_Million1(0);
gmm.Mega_Million2(0);
gmm.Mega_Million3(0);
gmm.Mega_Million4(0);
gmm.Mega_Million5(0);
gmm.Mega_Million6(0);
}
All replies (5)
Tuesday, September 8, 2015 11:03 PM âś…Answered
Hi bthJ6
From the view, how do you call a class method? I have a domain model, a void method in the controller.
According to your description, I'm not sure what you mean. If you would like to call a method in your controller. You could refer to the following code.
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public static string GetMega_Million()
{
return "123";
}
public ActionResult MegaMillion()
{
return View();
}
}
}
@using MvcApplication1.Controllers
@{
ViewBag.Title = "MegaMillion";
}
<h4>@HomeController.GetMega_Million()</h4>
If you'd like to define a class in the model and call his method in the view. You could try with the code below.
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult MegaMillion()
{
MegaMillion gmm = new MegaMillion();
return View(gmm);
}
}
}
@model MvcApplication1.Models.MegaMillion
@{
ViewBag.Title = "MegaMillion";
}
<h4>@Model.Mega_Million1(1)</h4>
I hope it's helpful to you.
Best Regards
Klein zhang
Wednesday, September 9, 2015 2:31 AM
Hi,
I think you are trying to call a method which is exist on the controller class..
So, you can use like this on View
@{
((TestController)this.ViewContext.Controller).Method1();
}
if your method is static then
@{
StaticClassName.Method2();
}
Hope this will help you.
Thanks
Saturday, September 19, 2015 6:20 AM
The method are public not static. The class is not static.
Tuesday, September 22, 2015 9:38 AM
Hi bthj6
bthJ6
The method are public not static. The class is not static.
In the above replies, i also give a sample that it define a public class in the model folder and create an instance of this class in our controller, the it pass the class to the view, finally we could call his method in the view.
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult MegaMillion()
{
MegaMillion gmm = new MegaMillion();
return View(gmm);
}
}
}
@model MvcApplication1.Models.MegaMillion
@{
ViewBag.Title = "MegaMillion";
}
<h4>@Model.Mega_Million1(1)</h4>
public class MegaMillion
{
public string Mega_Million1(int input)
{
// put your logic
return "Hello World";
}
}
I hope it's helpful to you.
Best Regards,
Klein zhang
Tuesday, October 20, 2015 12:13 AM
The method are public not static. The class is not static.
You can write C# code in the view itself. Here is how I would initiate the object and call the method (not a recommended way)
@{
var obj = new MegaMillion();
obj.GetMega_Million();
}