jeudi 2 août 2018

if else statement for Checking value (null or not) is not working - MVC

I'm trying to do simple if/else statement in my view to display/not display Bootstrap Modal and when Modal displayed it going to show some elements and then when button clicked its going to save some value to DB using AJAX.

as you see in Screen shots Confirmedby its NULL enter image description here

and then i do like this :

    @using NameSpace.Models
    @model VMRMA

                    @if (Model.Confirmedby == null)
                    {
                        <div data-toggle="modal" data-target="#myModaloplys">
                          Info
                        </div>

                    }

                   else
                    {
                       <div>Info<div>

                    }
<div id="myModaloplys" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
                        <div role="document" class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    &nbsp; <span id="exampleModalLabel" class="modal-title"><i style="color:#54e69d;" class="fa fa-check-square-o" aria-hidden="true"></i> Bekræfte sagen</span>
                                    <button type="button" data-dismiss="modal" aria-label="Close" class="close"><span id="x" title="Luk" style="text-shadow:none;" aria-hidden="true">×</span></button>
                                </div>
                                <div class="modal-body">
                                    <p><i class="fa fa-info-circle" aria-hidden="true"></i> Før du kommer i gang og behandler sagen, skal du først bekræfte sagen.</p>
                                    <form id="StatusForm">
                                        <div class="form-group">
                                            <label>Sagsnummer</label>
                                            <input type="text" value="@Model.HRMAs.Id" class="form-control disabled">
                                        </div>
                                        <div class="form-group">
                                            <label>Sag bekræftet af <span style="color:red;">*</span></label>
                                            <input type="text" id="Confirmedby" name="Confirmedby" placeholder="Indtast navn ..." class="form-control">
                                        </div>

                                        <input type="datetime" style="display:none;" id="ConfirmedDato" name="ConfirmedDato" value="@DateTime.Now.ToShortDateString()" />

                                    </form>
                                </div>
                                <div class="modal-footer">

                                    <button type="button" data-dismiss="modal" class="btn btn-secondary">Luk</button>
                                    <button type="submit" id="btnConfirmed" class="btn btn-primary">Bekræfte</button>

                                </div>
                            </div>
                        </div>
                    </div>

If part is going to work and its displaying Bootstrap Modal.

and then i modify Confirmedby to contains some data enter image description here

but this time else part not going to work ,it will display Modal again (which i dont want to)

Can anyone help me or point me in the right direction! :)
Thanks in advance :)

JavaScript:

 $(function () {
        $("#btnConfirmed").click(function (e) {
            e.preventDefault();

            var model = {

                ConfirmedDato: $("#ConfirmedDato").val(),
                Confirmedby: $("#Confirmedby").val(),
                id: $("#id").val(),
            }


                $.ajax({
                    type: 'POST',
                    url: "/User/confirmedRMACase",
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify(model),
                    success: function (run) {

                        if (!$.trim(run)) {
                            alert("What follows is blank: ");
                        }
                        else {


                            run.ConfirmedDato = model.ConfirmedDato;
                            run.Confirmedby = model.Confirmedby;


                        }


                    },

                    error: function () {
                        console.log('something went wrong - debug it!');
                    }

                });


        });


    });
</script>

Controller:

public JsonResult confirmedRMACase(int? id, DateTime? ConfirmedDato, string Confirmedby)
{

    var con = db.RMA_History.Where(t => t.Id == id).FirstOrDefault();



    if (con != null)
    {
        con.Confirmedby = Confirmedby;
        con.ConfirmedDato = ConfirmedDato;
        db.SaveChanges();

    }



    return Json(con, JsonRequestBehavior.AllowGet);


}

ViewModal VMRMA:

 public class VMRMA
    {
     public string Confirmedby { get; set; }
    }

Aucun commentaire:

Enregistrer un commentaire