使用 Html 按钮调用 MVC 控制器操作
Calling MVC Controller Action using Html button
我是 MVC.I 的新手,有一个按钮 "Create"。我想创建另一个在我的控制器中调用 "Deploy" 动作的按钮。这个按钮基本上是提交表单并被部署。当前,表单已提交,但代码未进入 "Deploy" 函数
这是我尝试过的:-
<input type="submit" class="btn btn-primary" value="Create" />
<input type="submit" class="btn btn-primary" value="Create and deploy" onclick="location.href='@Url.Action("Deploy", "MyController")' "/>
我的 Deploy 函数需要这样的参数:-
<a href="@Url.Action("Deploy", new {Id = Model.Id })">Deploy</a>
编辑
这是我的代码现在的样子:-
@using (Html.BeginForm("Create", "MyController", FormMethod.Post, new { data_bind = "submit: formSubmit" }))
{
<input type="submit" name ="create" class="btn btn-primary" value="Create" />
<input type="submit" name="create" class="btn btn-primary" value="Create and deploy"/>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyModel entity, string submitType)
{
if (submitType == Create and Deploy)
{
RedirectToAction("Deploy", "MyController");
}
//Code to create
}
public ActionResult Deploy(string Id)
{
}
我该如何解决这个问题?
使用这种方式点击部署操作
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
@using (Html.BeginForm("Create", "MyController", FormMethod.Post))
{
<input type="submit" name ="create" class="btn btn-primary" value="Create" />
}
或者您需要更改单击按钮时的表单操作,例如
function deploybtnClick{
document.getElementById('formId').action = 'Deploy';
}
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
@using (Html.BeginForm("Create", "MyController", FormMethod.Post))
{
<input type="submit" name ="create" class="btn btn-primary" value="Create" />
}
如果您给它一个 name
属性,提交按钮的值将 post 返回。将按钮的 html 更改为
<input type="submit" class="btn btn-primary" name="submitType" value="Create" />
<input type="submit" class="btn btn-primary" name="submitType" value="Create and deploy" />
控制器方法
public ActionResult Create(MyModel entity, string submitType)
{
if (!ModelState.IsValid)
{
return View(entity);
}
// Save the model
....
// Check which button submitted the form
if(submitType == "Create")
{
// redirect somewhere ?
}
else
{
// assuming you want to pass the value of entity.Id to the Deploy() method
RedirectToAction("Deploy", "MyController", new { Id = entity.Id });
}
}
为每个按钮添加一个客户端功能。
函数将有 ajax 调用,您可以使用所需的 ActionName 和 ControllerName
设置 Url 属性
我是 MVC.I 的新手,有一个按钮 "Create"。我想创建另一个在我的控制器中调用 "Deploy" 动作的按钮。这个按钮基本上是提交表单并被部署。当前,表单已提交,但代码未进入 "Deploy" 函数
这是我尝试过的:-
<input type="submit" class="btn btn-primary" value="Create" />
<input type="submit" class="btn btn-primary" value="Create and deploy" onclick="location.href='@Url.Action("Deploy", "MyController")' "/>
我的 Deploy 函数需要这样的参数:-
<a href="@Url.Action("Deploy", new {Id = Model.Id })">Deploy</a>
编辑
这是我的代码现在的样子:-
@using (Html.BeginForm("Create", "MyController", FormMethod.Post, new { data_bind = "submit: formSubmit" }))
{
<input type="submit" name ="create" class="btn btn-primary" value="Create" />
<input type="submit" name="create" class="btn btn-primary" value="Create and deploy"/>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyModel entity, string submitType)
{
if (submitType == Create and Deploy)
{
RedirectToAction("Deploy", "MyController");
}
//Code to create
}
public ActionResult Deploy(string Id)
{
}
我该如何解决这个问题?
使用这种方式点击部署操作
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
@using (Html.BeginForm("Create", "MyController", FormMethod.Post))
{
<input type="submit" name ="create" class="btn btn-primary" value="Create" />
}
或者您需要更改单击按钮时的表单操作,例如
function deploybtnClick{
document.getElementById('formId').action = 'Deploy';
}
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
@using (Html.BeginForm("Create", "MyController", FormMethod.Post))
{
<input type="submit" name ="create" class="btn btn-primary" value="Create" />
}
如果您给它一个 name
属性,提交按钮的值将 post 返回。将按钮的 html 更改为
<input type="submit" class="btn btn-primary" name="submitType" value="Create" />
<input type="submit" class="btn btn-primary" name="submitType" value="Create and deploy" />
控制器方法
public ActionResult Create(MyModel entity, string submitType)
{
if (!ModelState.IsValid)
{
return View(entity);
}
// Save the model
....
// Check which button submitted the form
if(submitType == "Create")
{
// redirect somewhere ?
}
else
{
// assuming you want to pass the value of entity.Id to the Deploy() method
RedirectToAction("Deploy", "MyController", new { Id = entity.Id });
}
}
为每个按钮添加一个客户端功能。
函数将有 ajax 调用,您可以使用所需的 ActionName 和 ControllerName
设置 Url 属性