我是MVC新手。我有一个按钮"创建"。我想创建另一个按钮来调用" 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")' "/>
我的部署功能需要这样的参数: -
<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)
{
}
我该如何解决这个问题?
答案 0 :(得分:1)
使用这种方式来点击部署行动
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
答案 1 :(得分:1)
如果您为其提供name
属性,则提交按钮的值将会回发。将按钮的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 });
}
}
答案 2 :(得分:0)
@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';
}
答案 3 :(得分:0)
@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" />
}
答案 4 :(得分:0)
对于每个按钮添加客户端功能。
函数将具有ajax调用,您可以使用所需的ActionName和ControllerName设置Url属性