![](/img/trans.png)
[英]Checkbox changestate on mouseover C# ASP.NET JAVASCRIPT
[英]ASP.NET get checkbox values to c# method
我對asp.net和atm都很缺乏經驗我在.cshtml視圖中有以下html和javascript代碼
<div class="jumbotron">
</div>
<div class="row">
<h2>Select the records you want to export into an excel sheet.</h2>
<hr />
<form name="recordSelection" method="POST" action="" >
<p>Press the "Download" button after you've selected the needed records.</p>
<blockquote>
<input type="checkbox" name="record" value="German Sales Office"/> German Sales Office<br/>
<input type="checkbox" name="record" value="UK Sales Office"/> UK Sales Office<br/>
<input type="checkbox" name="record" value="France Sales Office"/> France Sales Office<br/>
<input type="checkbox" name="record" value="Spain Sales Office"/> Spain Sales Office<br/>
<input type="checkbox" name="record" value="Hoofddorp Office"/> Hoofddorp Office<br/>
<input type="checkbox" name="record" value="Brussels Office"/> Brussels Office<br/>
</blockquote>
<hr/>
<input id="dlBttn" name="submitButton" type="submit" value="Download" onclick="GetCheckboxValues();"/>
</form>
</div>
<script type="text/javascript">
function GetCheckboxValues(form) {
var checkedValues = [];
var offices = document.getElementsByName('record');
for (var i = 0; offices[i]; i++) {
if (offices[i].checked) {
checkedValues.push(offices[i].value);
}
}
}
</script>
我需要選中復選框的值來知道我必須從數據庫中獲取哪些記錄,然后創建包含這些記錄的Excel工作表。 但我不知道如何將這些值用於我的C#代碼。 有關如何從我的javascript代碼調用C#方法的任何建議? 或者我是否必須創建一個Controller ActionResult
?
我通過評論中的有用建議解決了我的問題。 我通過將表單的action
屬性指定為action="@Url.Action("ExportData","MyController")"
將表單發布到方法public ActionResult ExportData(string[] record)
。 獲取從客戶端到服務器的復選框值,並能夠使用我的其余代碼進行處理。 (還刪除了腳本,因為它沒有做任何有用的事情)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.