2017-06-06 64 views
0

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

那你有什麼問題? –

+0

你是什麼意思_無需妥協other_?而你有'@using(Html.BeginForm())'這意味着你的形式將提交回POST方法具有相同的名稱爲GET方法(你沒有說是什麼) - 你會需要一點一個javascript回發到單獨的方法(雖然它不清楚爲什麼你需要單獨的方法) –

+0

@Stephen Muecke,更新後。 – leahsaif

回答

0

終於明白了。雖然@Stephen Muecke並沒有真正提供直接的解決方案,但我應該說他讓我重新考慮再次使用形式,所以我仍然要感謝他。謝了哥們!下面是我如何解決它:

查看

@using Models.Enums 
@model MyApp.Data.ApplicationSettings 

@Html.AntiForgeryToken() 
@using (Html.BeginForm()) 
{ 
    ... 
    <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System1" formaction="Admin/TestConnection" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button> 
    ... 
    <button type="submit" class="btn btn-warning pull-right" name="database" value="@DatabaseSystems.System2" formaction="Admin/TestConnection" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button> 
} 

控制器

public ActionResult TestConnection(ApplicationSettings model, DatabaseSystems database) 
{ 
    // at the end, instead of putting return View() or return("Index", model), I used below code 
    return Redirect("/[ControllerName]"); 
{ 

......這樣一來,formaction會做的ActionResult方法的調用對我來說,同時保持該按鈕的name屬性可用於其他用途,例如將其作爲參數傳遞給我的TestConnection方法。

0

使用單一的動作名稱提交給button.and的名字是在這兩個按鈕相同,但其值不同

進行檢查connectin

<button type="submit" class="btn btn-warning pull-right" name="save" value="@DatabaseSystems.System3" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Test Connection</button> 

用於保存

<button type="submit" class="btn btn-warning pull-right" name="Save" value="save" data-toggle="collapse" data-target="#loading"><span class="glyphicon glyphicon-link"></span> Save</button> 


    [HttpPost] 
    ActionResult Save(ApplicationSettings model,string Save) 
       { 
    if(Save=="Testconnection") 
    { 
    //do your code on save 
    } 
    if(save="Save") 
    { 
    //do your code on save 
    } 
     } 
+0

恐怕這一個不工作了隊友。控制器不能識別哪個方法正在被調用。 – leahsaif

+0

我修改了我的答案,請檢查。 – NIts577

+0

@ Nlts577,感謝您的建議,但這正是我想要避免的。我想讓MVC爲我做分隔,而不是我做這樣的代碼。 :d – leahsaif