2

下面是我的視圖中出現的一組單選按鈕。我能夠通過這個簡單的代碼檢索選定的項目從ASP MVC 3控制器設置單選按鈕檢查項目

string criteria = filter["criteria"]; 

但是,我不知道如何保留選定的項目。一旦控制器回到視圖,默認的單選按鈕總是被選中。

<form method="post"> 
     @Html.TextBox("searchValue", ViewBag.CurrentFilter as string, new { placeholder = "Search" }) 
     <input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" /> 
      <span class="error" style="clear: both;"> 
       @ViewBag.ErrorMessage 
      </span> 
     <a href="#" style="padding-left: 30px;"></a> 
     <br /> 
     <br /> 
     <input type="radio" name="criteria" id="bankName" value="bankName" checked="true"/> 
     <label for="bankName">Bank Name</label> 
     <input type="radio" name="criteria" id="EPURL" value="EPURL" /> 
     <label for="EPURL">EPURL</label> 
     <input type="radio" name="criteria" id="specialNotes" value="specialNotes" /> 
     <label for="SpecialNotes">Special Notes</label> 
     <input type="radio" name="criteria" id="email" value="email" /> 
     <label for="email">Email</label> 
     <input type="radio" name="criteria" id="dynamicsId" value="dynamicsId" /> 
     <label for="dynamicsId">Dynamics ID</label> 
     <input type="radio" name="criteria" id="stat" value="stat" /> 
     <label for="fixed">Agent ID &nbsp;</label> 
    </form> 
+0

對不起,我在下面找到了一個更簡單的解決方案。不得不等待幾天才能接受答案。 – NealR

回答

5

回答這個問題清盤是難以置信的簡單。我犯的第一個錯誤是沒有使用@Html控件。其次是使用FormCollection作爲索引控制器的輸入參數。通過改變單選按鈕來執行以下操作:

 @Html.RadioButton("criteria", "bankName", true)<span>Bank Name</span> 
     @Html.RadioButton("criteria", "EPURL")<span>EPURL</span> 
     @Html.RadioButton("criteria", "specialNotes")<span>Special Notes</span> 
     @Html.RadioButton("criteria", "email")<span>Email</span> 
     @Html.RadioButton("criteria", "dynamicsId")<span>Dynamics ID</span> 
     @Html.RadioButton("criteria", "stat")<span>Agent ID</span> 

和控制器中的Index方法的此簽名:

public ActionResult Index(string criteria, string searchValue) 

的選中的單選按鈕呆後背部後選擇。

1

如果我理解你的需求正確,你可以設置要在ViewBag(或ViewData的或模型)檢查項目的名稱和相應的設置在視圖中選中屬性。事情是這樣的:

<form method="post"> 
     @Html.TextBox("searchValue", ViewBag.CurrentFilter as string, new { placeholder = "Search" }) 
     <input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" /> 
      <span class="error" style="clear: both;"> 
       @ViewBag.ErrorMessage 
      </span> 
     <a href="#" style="padding-left: 30px;"></a> 
     <br /> 
     <br /> 

     @{ 
      var checkedItemName = ViewBag.CheckedItemName; 
     } 
     <input type="radio" name="criteria" id="bankName" value="bankName" checked="@((checkedItemName == "bankName"))"/> 
     <label for="bankName">Bank Name</label> 
     <input type="radio" name="criteria" id="EPURL" value="EPURL" checked="@((checkedItemName == "EPURL"))"/> 
     <label for="EPURL">EPURL</label> 
     <input type="radio" name="criteria" id="specialNotes" value="specialNotes" checked="@((checkedItemName == "specialNotes"))"/> 
     <label for="SpecialNotes">Special Notes</label> 
     <input type="radio" name="criteria" id="email" value="email" checked="@((checkedItemName == "email"))"/> 
     <label for="email">Email</label> 
     <input type="radio" name="criteria" id="dynamicsId" value="dynamicsId" checked="@((checkedItemName == "dynamicsId"))"/> 
     <label for="dynamicsId">Dynamics ID</label> 
     <input type="radio" name="criteria" id="stat" value="stat" checked="@((checkedItemName == "stat"))"/> 
     <label for="fixed">Agent ID &nbsp;</label> 
    </form> 
+0

這將正確的值傳遞迴視圖,但由於某種原因,最後一個單選按鈕始終處於選中狀態。 – NealR

+0

這是非常奇怪的,因爲如果我選擇'EPURL',這就是特定單選按鈕的呈現方式:' ' – NealR

1

不刷新整個頁面...使用jQuery,做一個Ajax調用

$.ajax({ 
    url: "Your Url", 
    async: true, 
    type: 'POST', 
    data: JSON.stringify({ ParameterName: Param_Value }), 
    beforeSend: function (xhr, opts) { 

    }, 
    contentType: 'application/json; charset=utf-8', 
    complete: function() { }, 
    success: function (data) { 
     //Success Callback 
     //Code to set value to your control 
    } 
});