HTML <button> onclick无法在<form>标记内工作

时间:2017-08-31 15:23:34

标签: html asp.net asp.net-mvc razor

我目前正在构建一个ASP.NET MVC应用程序,我想知道一个按钮onclick函数不能在form标签内工作的原因是什么。我希望这个按钮是type =“button”而不是“submit”。此外,没有任何输入绑定到任何模型属性。此应用程序的整个目的是作为我的客户的模型模板。按钮onclick操作是调用我创建的控制器操作,除了返回视图之外什么都不做。我在其他视图上有相同的设置,按钮按预期工作但由于某些原因,这些按钮在单击时不执行任何操作。我甚至复制并粘贴其他我知道工作的视图中的代码,但仍然没有。不知道这里发生了什么。任何帮助将不胜感激。谢谢!以下是一些示例代码:

CSHTML:

<div class="row">
        <div class="col-sm-offset-1 col-sm-10 col-lg-offset-1 col-lg-10">
            <h3>WORK LOCATION INFO</h3>
            <hr />
            <form>
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-3">
                            <label class="input-label" for="locationType">This is a:</label>
                            <select class="form-control" id="locationType">
                                <option></option>
                                <option>All location types</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-4">
                            <label class="input-label" for="campus">Campus <small><i style="color:red" class="fa fa-asterisk" aria-hidden="true"></i></small></label>
                            <select class="form-control" id="campus">
                                <option></option>
                                <option>All campuses</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="input-label" for="location">Location <small><i style="color:red" class="fa fa-asterisk" aria-hidden="true"></i></small></label>
                            <select class="form-control" id="location">
                                <option></option>
                                <option>All Locations</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-1">
                        <button type="button" class="btn btn-default button-blue" onclick="location.href='@Url.Action("WorkPosition", "RosterAdmin")'"><strong>GO BACK</strong></button>
                    </div>
                    <div class="col-md-2">
                        <button type="button" class="btn btn-default button-green" onclick="location.href='@Url.Action("CreateProfile", "RosterAdmin")'"><strong>CONTINUE</strong></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

控制器:

public class RosterAdminController : Controller
{
    // GET: RosterAdmin
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult WorkPosition()
    {
        return View();
    }

    public ActionResult WorkLocation()
    {
        return View();
    }

    public ActionResult CreateProfile()
    {
        return View();
    }

}

1 个答案:

答案 0 :(得分:1)

似乎location被设置为脚本中的其他位置。通常,使用window.location来避免这样的问题会更安全。如果您将按钮更改为使用window.location.href,则可能会有效。

您可以尝试在浏览器中调试此问题,以确定如果您希望在代码中的其他位置避免此更改location

请参阅:Is there any difference with using only location vs using window.location across browsers

相关问题