繁体   English   中英

识别在mvc 5中单击哪个按钮

[英]identify which button is being clicked in mvc 5

我正在使用MVC 5创建一个Web应用程序,这里我有两个按钮

<div class="row">
    <div class="col-sm-offset-5 col-sm-1">
        <input type="submit" name="save" value="SAVE" class="btn btn-primary glyphicon glyphicon-save" />
    </div>
    <div class="col-sm-offset-0 col-sm-1">

            <input type="submit" name="reset" id="reset" value="Reset" class="btn btn-warning active glyphicon glyphicon-refresh" />
    </div>                        
</div>

我有一种方法,单击按钮后会被调用,

现在在该事件上,我想区分按钮单击,

喜欢,

如果用户点击

<input type="submit" name="save" value="SAVE" class="btn btn-primary glyphicon glyphicon-save" />

然后

{
//this code should run
}

如果用户点击

<input type="submit" name="reset" id="reset" value="Reset" class="btn btn-warning active glyphicon glyphicon-refresh" />

然后

{ //this set of code should run }

我的方法看起来像这样

[HttpPost]
        public ActionResult insertrecd(FormCollection fc)
        { 
            if(fc["reset"]==null)
            {
                return RedirectToAction("party", "party");
            }
            else
            {
                ViewBag.message = string.Format("Hello {0}.\\nCurrent Date and Time: {1}", "name", DateTime.Now.ToString());
                return RedirectToAction("party", "party");
            }
        }

我需要做什么,我想在不同的按钮单击上执行不同的代码?

为每个输入按钮指定相同的名称,但取不同的值

<input type="submit" name="action" value="save" class="btn btn-primary glyphicon glyphicon-save" />

<input type="submit" name="action" id="reset" value="reset" class="btn btn-warning active glyphicon glyphicon-refresh" />

该值将在您的表单集合中显示为

fc["action"]

所以你的控制器动作看起来像

[HttpPost]
public ActionResult insertrecd(FormCollection fc)
{
    var action = fc["action"];

    if(action == "save")
    {
        //save stuff
    }

    if(action =="reset")
    {
        //reset stuff
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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