我对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#方法的任何建议?还是我必须创建一个控制器之类的ActionResult
东西?
我通过评论中的有用建议解决了我的问题。我public ActionResult ExportData(string[] record)
通过将action
表单的属性指定为来将表单发布到方法中action="@Url.Action("ExportData","MyController")"
。为了从客户端到服务器获取复选框值,并能够处理其余的代码。(还删除了脚本,因为它没有任何用处)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句