【发布时间】:2017-06-06 03:51:02
【问题描述】:
我构建表单的方式是有多个“测试连接”提交按钮和一个“保存”提交按钮来保存每个测试连接的所有凭据和 URL。我已经看到了一些建议的解决方案,但我不想做 ajax 或 javascript。所以我想知道这是否可能。顺便说一句,我想通过提交按钮将值作为枚举值传递给控制器,并在我的控制器中接收该枚举。
我可以轻松创建多个 ActionResult,但对我来说感觉太复杂了,如果我必须更改测试连接中的某些内容,我就必须全部更改。所以我希望只有一个用于 TestConnection 的 ActionResult 方法加上一个 Save ActionResult 都在同一个表单中。
当我单击“保存”按钮时,所有凭据都将作为 ApplicationSettings 模型发送到控制器。
当我单击“测试连接”按钮时,我仍然会收到 ApplicationSettings 模型以及 DatabaseSystems 枚举,并且根据从按钮收到的枚举,我会进行必要的连接。
下面是sn-ps的代码,但我就是不能让它们工作。
尝试的方法:
1.空白表单动作,提交按钮的名称分别为“Save”和“TestConnection”。 - 保存作品。 TestConnection 不起作用,说找不到资源。
2.表单动作指向“TestConnection”,TestConnection按钮名称设置为
“数据库”,而值设置为 @DatabaseSystems.xxx,保存按钮设置为“action:Save”,添加了用于保存的 ActionResult 方法[MultipleButton(Name = "action", Argument = "Save")] - TestConnection 有效,Save 不起作用,说明请求不明确。
我似乎无法弄清楚使它们都工作的配置。 :(
查看
@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
}
枚举
public enum DatabaseSystems
{
System1,
System2,
System3,
... and many more systems
}
【问题讨论】:
-
那你有什么问题?
-
你是什么意思不损害对方?而且你有
@using (Html.BeginForm()),这意味着你的表单将提交回与 GET 方法同名的 POST 方法(你没有说那是什么) - 你需要一点 javascript 来回发到单独的方法(尽管不清楚为什么需要单独的方法) -
@Stephen Muecke,更新帖子。
-
我想远离一种 ActionResult 方法并在内部进行隔离。相反,我希望视图本身为我进行隔离,因此当我单击 TestConnection 时,它会直接转到 TestConnection 方法等。
-
我之前试过:Controller
public ActionResult TestConnectionSystem1(ApplicationSettings model, DatabaseSystems database)public ActionResult TestConnectionSystem2(ApplicationSettings model, DatabaseSystems database)public ActionResult TestConnectionSystem3(ApplicationSettings model, DatabaseSystems database)然后查看<button type="submit" class="btn btn-warning pull-right" name="action:TestConnectionSystem1" id="@DatabaseSystems.System1" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button>
标签: c# asp.net-mvc forms razor