Balugu
Balugu

Reputation: 23

How to access class method from view in asp.net mvc c#?

I have controller : RecruitmentController.cs and i have method/action : cekrecomended

    public string cekrecomended(string id)
    {
        id = EncryptDecrypt.Decrypt(id);
        string ada = "false";
        string jurusan = Session["Jurusan"].ToString();
        string pendidikan = Session["Pendidikan"].ToString();
        int usia = Convert.ToInt16(Session["Usia"]);
        string kelamin = Session["Kelamin"].ToString();
        double nilai = Convert.ToDouble(Session["Nilai"]);
        var data = (from c in db.ViewVacancyJabatanPerusahaans
                    where
                        c.Pendidikan.Contains(pendidikan) &&
                        (c.Jurusan.Contains(jurusan) || c.Jurusan.Contains("Semua jurusan")) &&
                        c.Nilai <= nilai &&
                        c.UsiaDari <= usia &&
                        c.UsiaSampai >= usia &&
                        c.JenisKelamin.Contains(kelamin) &&
                        c.TglAkhirlamaran >= DateTime.Now &&
                        c.Dlt == false &&
                        c.IDVancancy == Convert.ToInt16(id)
                    orderby c.IDVancancy descending
                    select c).Count();
        if (data > 0)
        {
            ada = "true";
        }
        return ada;
    }

i want to access cekrecomended from view.

@if(Human_Resource_Development.Controllers.RecruitmentController.cekrecomended(Convert.ToString(item.IDVancancy)) == "true")
{
    <button>Apply this position</button>
}

but i get error.

Upvotes: 0

Views: 5278

Answers (3)

Balugu
Balugu

Reputation: 23

Thank you for your answers. My problem solved, i have solution. i just change

public string cekrecomended(string id)
{
    //bla bla
}

to

public static string cekrecomended(string id)
{
    //bla bla
}

and i can access from view.

Upvotes: 0

ekad
ekad

Reputation: 14614

You should use your view model for that purpose. Let's say MyViewModel is the class of your model, add a boolean property named IsRecommended

public class MyViewModel
{
    // all of the other properties
    // ....

    public bool IsRecommended { get; set; }
}

Set the value of IsRecommended in your controller action method

public ActionResult Index()
{
    MyViewModel model = new MyViewModel();

    // other codes here
    // ...

    model.IsRecommended = ....; // logic from cekrecomended method here

    return View(model);
}

Make sure you have this syntax at the top of your view

@model MyViewModel

and determine whether the "Apply this position" button will be displayed or not based on the value of IsRecommended property as below

@if (Model.IsRecommended)
{
    <button>Apply this position</button>
}

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160902

This defeats the separation of model, view and controller MVC is based on - in your view you should just use your view model. In your case just use a view model that has a property making the result of this method call available.

Upvotes: 1

Related Questions