Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass enum value to controller with ajax?

I want to pass my enum value to controller.

I have this enum in my model:

public enum Section
{
      Upper,
      Lower
};

I want to pass this value through ajax on my controller:

$(function () {
  var section='@Section.Upper'//i want to pass Upper value only to my controller.
  alert(section) ///output Upper
 $.ajax({
                url: '/Section/FindSection',
                data: { 'Section': section},
 });

In Database table it is storing 1 for upper and 0 for lower.

My Controller:

public ActionResult Generate(int FindSection)
{   
}

Error:The parameters dictionary contains a null entry for parameter 'FindSection' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Generate(Int32)'

I know i can pass directly pass 1 but i dont want to hardcode because in future if i need to pass any other thing then again i have to change the code.

how can i do this???

like image 483
Pratik Soni Avatar asked Nov 22 '25 05:11

Pratik Soni


1 Answers

You just need to pass the index of the item you want.

public ActionResult Generate(Section section)
{   

}

$(function () {
  //...
 $.ajax({
            url: '/Section/Generate',
            data: { section: 1} //the controller will receive Super.Lower
 });
 //..
})
like image 170
Júlio Murta Avatar answered Nov 24 '25 20:11

Júlio Murta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!