0

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.

1
  • 1
    You shouldn't do this at all - just expose the right calculated property in your view model and use it Commented Apr 6, 2015 at 2:32

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>
}

Comments

0

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.

Comments

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.