简体   繁体   中英

How to call client side click on html submit button in mvc

Hello Friends,
please help me i am new in MVC Dot net, and i am try to insert label value on database i have a two labels first for IP address and Second for Current running time and three submit buttons checkin, checkout and going for personal task. on page load checkout and personal task buttons are hidden, only show 2 labels and check in button.
when i am clicking on checkin button both labels value stored in database ms sql2008 . and automatically checkout and personal task button should be enabled and check in button should be disabled.
when i am doing this task with jquery or java script on click event then my insertion task on controller not working only jquery worked.

please help me how i can calling client side click event with html submit button..

my code is: on view -->

 <button id="btnchkin" value="CheckIn" type="submit" name="action"/>
  <input type="submit"  id="btntask" name="action"  value="PersonalTask" />
   <input type="submit" id="btnchkout" name="action"  value="CheckOut" />

on script tag -->

 $("#btnchkin").click(function () {

    alert("a");
    //  $('#btnchkin').prop('disabled', true);
    $('#btntask').prop('disabled', false);
    $('#btnchkout').prop('disabled', false);
    $('#btnchkin').prop('disabled', true);

    //alert("b");
});

and controller-->

[HttpPost]
[ActionName("Index")]
public ActionResult Index( string lblIP1)
{

        DateTime datetime = DateTime.Now;
        string date = datetime.ToString();
        date = datetime.Date.ToString();
        lblIP1 = ipp;
        string ip = lblIP1;

        string s = DateTime.Now.ToString("hh:mm:ss tt");

        EmployeeSchedule emp = new EmployeeSchedule();
        emp.IP = ip;
        emp.CurrentDate = Convert.ToDateTime(date);
        emp.CheckInTime = s.ToString();

        BAL_EmployeeSchedule employeeBusinessLayers = new BAL_EmployeeSchedule();
        employeeBusinessLayers.AddEmployee(emp);

        ViewBag.ip = ipp;


    return View();
}

Here is your controller method:

public ActionResult Index()
{
    // return view to show three buttons...
    ViewBag.IsCheckedIn = false;
    return View();
}

Here is your view:

<script type="text/javascript">
    $(document).ready(function() {
        @if (ViewBag.IsCheckedIn)
        {
            <text>
                $('#btntask').prop('disabled', false);
                $('#btnchkout').prop('disabled', false);
                $('#btnchkin').prop('disabled', true);
            </text>
        }
        else
        {
            <text>
                $('#btntask').prop('disabled', true);
                $('#btnchkout').prop('disabled', true);
                $('#btnchkin').prop('disabled', false);
            </text>
        }
    });
</script>
<button id="btnchkin" value="CheckIn" type="submit" name="action"/>
<input type="submit"  id="btntask" name="action"  value="PersonalTask" />
<input type="submit" id="btnchkout" name="action"  value="CheckOut" />

Here is your post controller method:

[HttpPost]
public ActionResult Index()
{
    ViewBag.IsCheckedIn = true;
    return View();
}

Please let me know if this is what you wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM