我構建表單的方式是有多個「測試連接」提交按鈕和一個「保存」提交按鈕,以保存每個測試連接的所有憑據和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