1

I would like to disable a cancel button if the the record field "Status" is already recorded as cancelled. I already know how to disable a button but the problem is how would the jquery know that the record field "Status" is Cancelled. Here Are the Codes

 @foreach (var rDetail in Model.Customers.ToList()) { 

      <tr>
    <td>
        @Html.DisplayFor(model => rDetail.DateEntry)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.DateStart)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.DateEnd)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.Status.Name)
    </td>

    <td>
        @Html.DisplayFor(model => rDetail.UserCode)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.DateModified)
    </td>
    <td>
        @Html.DisplayFor(model => rDetail.Remarks)
    </td>
    <td>
        @Html.ActionLink("Details", "Details", "RoomReservation", new { id = rDetail.Id}, null) |
        @using (Html.BeginForm("CancelReservation", "Rooms", new { roomId = Model.Id, reservationId = rDetail.Id, methodId = 0})) {
        <input type="submit" value="Cancel" class ="cancelSubmit"/>
        }
    </td>
</tr>

Any help will be appreciated, thanks :)

1
  • You want to make the button disabled when the status label is cancelled right? That too during render? Commented May 10, 2013 at 2:39

2 Answers 2

1

If you know the status is cancelled you can disable it in the Razor itself.

    <td>
        @Html.ActionLink("Details", "Details", "RoomReservation", new { id = rDetail.Id}, null);

  @if(rDetail.Status.Name.Equals("cancelled"))
  {
    <input type="submit" value="Cancel" class ="cancelSubmit" disabled/>
  }
  else
  {
        @using (Html.BeginForm("CancelReservation", "Rooms", new { roomId = Model.Id, reservationId = rDetail.Id, methodId = 0})) {
        <input type="submit" value="Cancel" class ="cancelSubmit"/>
        }
   }
    </td>

If you want to do it jquery way:-

$(function(){
$('.cancelSubmit').each(function(){
   if($(this).closest('tr').find('#Status_Name').text() === 'cancelled')
   {
       $(this).prop('disabled',true);
   }
  });
 });

or inside the function you could do :-

$(this).prop('disabled',
        $(this).closest('tr')
       .find('#Status_Name')
       .text() === 'cancelled');
Sign up to request clarification or add additional context in comments.

4 Comments

Dude, you're a life saver. I used the razor method and it worked like a charm. Though I also tried the logic before but we differ on code syntax. I really need to increase my code dictionary on ASP.net MVC 4. Anyway, thanks again man.
Wow great, glad it worked. I just typed it in the editor itself, wasn't even sure syntax is right.. :)
Well, at first it didn't worked but your "cancelled" is in small letter "c" as for my records where on a big letter "C", after changing it, it worked ^^
Use StringComparison.InvariantCultureIgnoreCase. i.e rDetail.Status.Name.Equals("cancelled",StringComparison.InvariantCultureIgnoreCase); So case won't matter.
0

If I understand you, something like this should work:

$('#Status_Name').on('keypress', function(e) { //I think that's how the XFor handlers format ids
    $('button_to_disable').prop('disabled', this.value === 'Cancelled');
});

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.