我构建表单的方式是有多个“测试连接”提交按钮和一个“保存”提交按钮,以保存每个测试连接的所有凭据和URL。我看到了一些建议的解决方案,但我不想做一个Ajax或JavaScript。所以我想知道这是否可能。顺便说一下,我想通过提交按钮将值作为枚举值传递给控制器,并在控制器中接收该枚举。多个提交按钮与从按钮传递的值
我可以很容易地做出多个ActionResults,但它对我来说只是感觉如此复杂,如果我必须改变测试连接中的某些东西,我将不得不改变所有。所以我想在同一个表单中只有一个TestConnection的ActionResult方法和一个Save ActionResult。
当我点击保存按钮时,所有凭证都将作为ApplicationSettings模型发送到控制器。
当我单击测试连接按钮时,我仍然收到ApplicationSettings模型加上DatabaseSystems枚举,并根据接收到的按钮的枚举,我会做必要的连接。
下面是代码片段,但我不能让它们工作。
尝试的方法: 1.空白表单动作,提交按钮的名称相应地为“Save”和“TestConnection”。 - 保存作品。 TestConnection不起作用说无法找到资源。 2.表单操作指向“TestConnection”,TestConnection按钮名称设置为 “database”,值设置为@ DatabaseSystems.xxx,Save按钮设置为“action:Save”,添加Action的ActionResult方法[MultipleButton(Name = "action", Argument = "Save")]
- TestConnection工作,保存不起作用的含糊不清的要求。
我似乎无法弄清楚配置,使他们都工作。 :(
查看
@using Models.Enums
@model MyApp.Data.ApplicationSettings
@Html.AntiForgeryToken()
@using (Html.BeginForm())
{
<div class="row">
<div class="col-md-2">
@Html.EditorFor(model => model.con1_un, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con1_un) } })
</div>
<div class="col-md-2">
@Html.EditorFor(model => model.con1_pw, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con1_pw) } })
</div>
<div class="col-md-7">
@Html.EditorFor(model => model.con1_url, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con1_url), @rows = "1", @style = "max-width:520px !important" } })
</div>
<div class="col-md-1 pull-right">
<button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System1" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button>
</div>
</div>
<div class="row">
<div class="col-md-2">
@Html.EditorFor(model => model.con2_un, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con2_un) } })
</div>
<div class="col-md-2">
@Html.EditorFor(model => model.con2_pw, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con2_pw) } })
</div>
<div class="col-md-7">
@Html.EditorFor(model => model.con2_url, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con2_url), @rows = "1", @style = "max-width:520px !important" } })
</div>
<div class="col-md-1 pull-right">
<button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System2" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button>
</div>
</div>
<div class="row">
<div class="col-md-2">
@Html.EditorFor(model => model.con3_un, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con3_un) } })
</div>
<div class="col-md-2">
@Html.EditorFor(model => model.con3_pw, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con3_pw) } })
</div>
<div class="col-md-7">
@Html.EditorFor(model => model.con3_url, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.con3_url), @rows = "1", @style = "max-width:520px !important" } })
</div>
<div class="col-md-1 pull-right">
<button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System3" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button>
</div>
</div>
... and many more test connections for different systems
<button type="submit" class="btn btn-success pull-right" name="Save" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
}
控制器
[HttpPost]
ActionResult TestConnection(ApplicationSettings model, DatabaseSystems database)
{
// depending on the DatabaseSystems enum passed, then I'll do the necessary connection check.
{
[HttpPost]
ActionResult Save(ApplicationSettings model)
{
// Save credentials entered in all the input fields above for con1, con2 & con3.
}
型号
[Serializable]
public class ApplicationSettings
{
public string con1_un { get; set; }
public string con1_pw { get; set; }
public string con1_url { get; set; }
public string con2_un { get; set; }
public string con2_pw { get; set; }
public string con2_url { get; set; }
public string con3_un { get; set; }
public string con3_pw { get; set; }
public string con3_url { get; set; }
... and many more systems
}
恩UM与多个不同的参数
public enum DatabaseSystems
{
System1,
System2,
System3,
... and many more systems
}
那你有什么问题? –
你是什么意思_无需妥协other_?而你有'@using(Html.BeginForm())'这意味着你的形式将提交回POST方法具有相同的名称为GET方法(你没有说是什么) - 你会需要一点一个javascript回发到单独的方法(虽然它不清楚为什么你需要单独的方法) –
@Stephen Muecke,更新后。 – leahsaif