剃刀输入有两个动作

本文关键字:两个 输入 剃刀 | 更新日期: 2023-09-27 17:53:21

我有一个简单的提交输入:

@using (Html.BeginForm())
{
      <input type="submit" value="Save" id="SaveButton" class="btn" />
}

现在我想让这个按钮也成为一个热链接。当我点击它时,我想移动到"X"动作。

I tried with:

onclick="location.href='@Url.Action("Index", "Student")'"

但它只提交更改,并刷新实际页面。重定向工作,如果输入是@using(…)之外,但这当然没有帮助我。

剃刀输入有两个动作

我要做的是使用JavaScript提交表单:

在表单声明中添加onsubmit事件处理程序

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new
{       
    onsubmit = "javascript:onFormSubmit(this); return false;"
}))
{
    <!-- your form -->
} 
然后onsummitjavascript函数像这样提交表单:
function onFormSubmit(form)
{
    var data = new FormData($(form)[0]);
    if ($(form).valid())
    {
            $.ajax({
                url:  $(form).attr('action'),
                cache: false,
                processData: false,
                contentType: false,
                data: data,
                type: 'POST',
                error: function(jqXHR, textStatus, errorThrown)
                {
                    throw new Error('Cannot get action');
                }
            }).done(function(view)
            {
                // redirect here....
            });
        }
}

如果你不想这样做,在你的索引操作而不是返回视图这样做:

return Redirect("http://www.google.com");// or any url you want

另一种方法是重定向到不同的Action,如:

return RedirectToAction("RedirectAction");

然后在控制器中添加另一个名为"RedirectAction"的动作。

public ActionResult RedirectAction()
{
   // Do some stuff
    return Redirect("http://www.google.com");// or any url you want
}

相关文章: