【问题标题】:How to call client side click on html submit button in mvc如何调用客户端单击mvc中的html提交按钮
【发布时间】:2014-04-11 01:55:21
【问题描述】:

朋友们好,
请帮助我,我是 MVC Dot net 的新手,我正在尝试在数据库中插入标签值,我有两个标签,第一个是 IP 地址,第二个是当前运行时间,三个提交按钮签入、签出和执行个人任务。 页面加载结帐和个人任务按钮被隐藏,仅显示 2 个标签和签入按钮。
当我单击签入按钮时,两个标签值都存储在数据库 ms sql2008 中。并应启用自动结帐和个人任务按钮,并应禁用签入按钮。
当我在单击事件上使用 jquery 或 java 脚本执行此任务时,我在控制器上的插入任务不起作用,只有 jquery 起作用。

请帮助我如何使用 html 提交按钮调用客户端点击事件..

我的代码是: 正在查看 -->

 <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" />

在脚本标签上 -->

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

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

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

和控制器-->

[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();
}

【问题讨论】:

  • 您好,您是想在浏览器中按下按钮,然后通过 JSON 从服务器向客户端返回数据,还是希望页面重新加载并返回包含您数据的模型?跨度>
  • 不,我只想启用和禁用按钮
  • 我不知道那是什么意思。您希望启用和禁用哪些按钮,您希望它们在什么条件下如此?
  • 在服务器标签中 我们可以使用 OnClientClick 事件来使用 jquery 或 java script 。同样我想在 mvc 中使用这个
  • 当我点击第一个(签入)按钮时,其他 2 个按钮应启用,第一个按钮(签入)应禁用。在数据库中插入标签数据。其他 2 个按钮(结帐和个人任务按钮)在页面加载时被禁用。

标签: c# asp.net-mvc-4 razor


【解决方案1】:

这是你的控制器方法:

public ActionResult Index()
{
    // return view to show three buttons...
    ViewBag.IsCheckedIn = false;
    return 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" />

这是你的后控制器方法:

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

如果这是您想要的,请告诉我。

【讨论】:

  • yes @user1477388 我也想要这个,但是当我在做这个脚本标签时,我想要 $('#btnchkin') "Too many characters in character literal" 出现错误
  • 抱歉,您还必须为 Razor + javascript 添加&lt;text&gt;。查看我的编辑。
  • 如果您希望它作为 Razor 执行,则需要 @,但您可以将其删除,然后放入 if (@ViewBag.IsCheckedIn),它将作为 javascript 执行。让我知道结果如何。
  • 我刚刚学习了两天关于 MVC 的知识,我必须在两天内完成这项任务以完成我的工作..请帮助我..拜托..
  • 我当然会帮助你。但是,首先我必须知道问题是什么。如果你的代码和我的一样,那么它应该可以按照stackoverflow.com/questions/5614941/… 正常工作。您是否尝试过构建您的项目/解决方案?有时,IDE 会说有语法错误,而实际上没有错误。
猜你喜欢
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 2011-07-11
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多