Reputation: 85
I am new to MVC.I have a button "Create". I want to create another button which calls the "Deploy" Action in my controller. This button basically Submits the form and gets deployed. Currently, the form gets submitted but the code does not enter the "Deploy" function
Here is what I have tried:-
<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")' "/>
My Deploy function need a parameter something like this:-
<a href="@Url.Action("Deploy", new {Id = Model.Id })">Deploy</a>
EDIT
THis is how my code looks now:-
@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)
{
}
How can I solve this?
Upvotes: 1
Views: 568
Reputation: 771
For each button add an client side function.
Function will have ajax call, and you can set the Url Property with your required ActionName and ControllerName
Upvotes: 0
Reputation:
The value of submit button will post back if you give it a name
attribute. Change the html for the buttons to
<input type="submit" class="btn btn-primary" name="submitType" value="Create" />
<input type="submit" class="btn btn-primary" name="submitType" value="Create and deploy" />
and the controller method to
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 });
}
}
Upvotes: 1
Reputation: 29
@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" />
}
Upvotes: 0
Reputation: 6766
@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" />
}
Or you need to change the action of form on click of the button like
function deploybtnClick{
document.getElementById('formId').action = 'Deploy';
}
Upvotes: 0
Reputation: 29
use this way to hit to that deploy Action
@using (Html.BeginForm("Deploy", "MyController", FormMethod.Post))
{
<input type="submit" class="btn btn-primary" value="Create and deploy"/>
}
Upvotes: 1