我知道如何通过简单的形式将参数传递给控制器。但是当我在一个表单中有多个表单时,我不知道如何将正确的参数传递给控制器。在下面的示例代码中,用户应从选项列表中选择一个项目,然后单击“提交”,然后将所选值交给控制器。
查看:
@model PAM_Dashboard_Project.Models.UserClass
<h3 align="center" style="font-family:'Fidelity Sans';">CyberArk Application Links</h3>
@* Application Links *@
<div class="btn-group btn-group-justified">
<div class="btn-group col-lg-3">
<button type="button" class="btn btn-primary" onclick="window.location.href='/'" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">Production Environment</button>
</div>
<div class="btn-group col-lg-3">
<button type="button" class="btn btn-primary" onclick="window.location.href='/'" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">QA Environment</button>
</div>
</div>
<h3 align="center" style="font-family:'Fidelity Sans';">CyberArk Scripts </h3>
<form asp-controller="Home" asp-action="Index" method="post" role="form">
FistName: <input asp-for="firstName" /> <br />
LastName: <input asp-for="lastName" /><br />
<button type="submit">Register</button>
</form>
<div class="btn-group-vertical" style="width:100%; ">
<div class="btn-group">
<button type="button" class="btn btn-primary" id="formButton1" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">
<p>Monitoring </p>
<text style="font-size:80%;"> Scripts related to overseeing the status of the product</text>
</button>
@* Overall form for Monitoring *@
<form asp-controller="Home" asp-action="Index" method="post" role="form" onsubmit="return confirm('Do you really want to execute this script?');" id="form1" method="post" style="display:none;">
<div id="accordion" role="tablist" aria-multiselectable="true">
@* Form 1 *@
<div class="card">
<div class="card-header" role="tab" id="headingTwo">
<h5 class="mb-0">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" style="font-size:15px;">
Vault Status
</a>
</h5>
</div>
<div id="collapseTwo" class="collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="card-block">
<div class="form-group">
<div class="form-group">
<p> This script returns status of vault servers for the chosen environment. It is used to determine if any servers overlap in order to detect a split brain.</p>
</div>
<label for="exampleFormControlSelect2">Select Environment</label>
<select multiple class="form-control" id="exampleFormControlSelect2">
<option>RTP Prod <input asp-for="Vault" /> </option>
<option>OMA Prod</option>
<option>BGI</option>
<option>BG2</option>
<option>Cloud</option>
<option>Workstation</option>
<option>QA</option>
</select>
<br />
<button type="submit">Submit</button>
</div>
</div>
</div>
</div>
@* Form 2 *@
<div class="card">
<div class="card-header" role="tab" id="headingThree">
<h5 class="mb-0">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree" style="font-size:15px;">
Script 2
</a>
</h5>
</div>
<div id="collapseThree" class="collapse" role="tabpanel" aria-labelledby="headingThree">
<div class="card-block">
<div class="form-group">
<label for="exampleFormControlSelect2">Select Environment</label>
<select multiple class="form-control" id="exampleFormControlSelect2">
<option>RTP</option>
<option>OMA</option>
<option>Break glass 1</option>
<option>Break glass 2</option>
<option>Cloud</option>
<option>Workstation</option>
<option>QA</option>
</select>
<br />
<button>Submit</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="window.location.href='/'" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">
<p>Patching</p>
<text style="font-size:80%">Scripts used to patch or update a product</text>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="window.location.href='/'" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">
<p>Onboarding</p>
<text style="font-size:80%">Scripts related to onboarding new accounts to the product</text>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="window.location.href='/'" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">
<p>Disaster Recovery</p>
<text style="font-size:80%;">Scripts used to exclusively in disaster recovery situations</text>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="window.location.href='/'" style="background-color:#EEEEEE; border-color:darkgrey; color:black;">
<p>Reporting</p>
<text style="font-size:80%;">Scripts used to gather information for reporting purposes.</text>
</button>
</div>
</div>
控制器:
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpPost] //Specifies the method as a Post method
public IActionResult Index(UserClass userData) //specify the parameter is of type UserClass.
{
UserClass userInfo = new UserClass(); //Create a new object of type UserClass
//Assign the attributes of the passed object to the newly created object
userInfo.firstName = userData.firstName;
userInfo.lastName = userData.lastName;
//Return a string which includes the passed form data (object attribute)
return Content($"Welcome{userInfo.firstName} {userInfo.lastName}!");
}
[HttpPost] //Specifies the method as a Post method
public IActionResult Index(Test1 userData) //specify the parameter is of type UserClass.
{
Test1 userInfo = new Test1(); //Create a new object of type UserClass
//Assign the attributes of the passed object to the newly created object
Test1.Vault = userData.Vault;
//Return a string which includes the passed form data (object attribute)
return Content($"You chose {Test1.Vault}!");
}
}
型号:
public class UserClass
{
[Display(Name = "First Name")]
public string firstName { get; set; }
[Display(Name = "Last Name")]
public string lastName { get; set; }
}
public class Test1
{
[Display(Name = "RTP Prod")]
public string Vault { get; set; }
}