1

Here is my code. On click of the button I am trying to enter an enum flag value in to a database table, but I am getting a reference error.

<button type="button" class="btn btn-default clean" data-id="@item.RoomId">status</button>

$(function () {
  $('.clean').click(function () {
    $.post("@Url.Action("SetStatus", "ReceptionHk")", { 
      id: $(this).data("id"), 
      newStatus: @EnumHkStatus.Cleaning 
    });
  });
});
[Flags]
public enum EnumHkStatus
{
  None,
  Repair,
  Cleaning,
  InventoryCheck,
  Occupied
};

on click of the button i am trying to enter value in the status column as int value as it is in the enumerator with flags but i am getting following error

ReferenceError: Cleaning is not defined

Please help me with this

1 Answer 1

1

Take a look at what your server-side code is outputting to the browser. It sounds like this:

@EnumHkStatus.Cleaning

is outputting this:

Cleaning

Which isn't understood by the JavaScript code. (That's what the error is specifically telling you.) For your JavaScript code you'd probably either want that to be a string:

'@EnumHkStatus.Cleaning'

or an int:

@((int)EnumHkStatus.Cleaning)

(Which one is up to you. You may want to test both, I'm not sure if the model binder can automatically translate either or both of them back to the enum value.) So that ultimately your POST call would at least contain valid code. For example:

$.post("@Url.Action("SetStatus", "ReceptionHk")", { id: $(this).data("id"), newStatus: @((int)EnumHkStatus.Cleaning) });

would output something like:

$.post("/ReceptionHk/SetStatus", { id: $(this).data("id"), newStatus: 3 });

You might then need to update your SetStatus action to accept an int or string (whichever you chose), unless the model binding can convert to the corresponding enum for you. You'll want to test that. If you do need to change the method signature, just convert it back to the corresponding enum at the start of the action method.

Side note: You probably want to have some code to handle the AJAX response, if for no other reason than to check the success/failure of the operation.

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

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.