我有一些我正在维护的MVC4代码。以前的开发人员创建了一个包含搜索按钮和下载按钮的页面,两者都是提交按钮。发布页面后,他检查Request.Form("btnDownload") != null
以查看是否已单击下载按钮,这样可以正常工作但是该按钮导致的下载不返回视图,它返回CSVResult
,返回一个Excel的.csv
文件。因此,下次单击OTHER提交按钮时,btnDownload
元素仍然不为空,并且代码认为已单击下载按钮。
我尝试了另一种方法,我在Stack Overflow上找到了两个提交按钮(添加了一个具有不同值的Command
元素),但是当单击一个按钮时,该方法会禁用所有按钮。
我需要一种方法,可以使用JavaScript或任何其他方式从帖子变量中删除btnDownload
。
我简短,我有一个搜索按钮和一个下载按钮,这两个按钮都会导致帖子,但只有搜索按钮才能返回视图。如何在页面的开头放置一些JavaScript,使btnDownload
按钮处于初始状态,以便它不会在Request.Form
中返回?
这是代码。此代码在许多页面上重复使用,这只是一些搜索值文本框,搜索按钮,清除(重置搜索)按钮和下载按钮。
@{
var filters = MetadataProviderHelper.GetModelMetadataForSearchFilterFields(@Model);
var pageSize = (int)ViewBag.PageSize;
var pageIndex = (int)ViewBag.PageIndex;
var totalItemCount = (int)ViewBag.TotalItemCount;
var sortColumn = string.Empty;
var sortDir = string.Empty;
}
<script type="text/javascript">
$().ready(function () {
$('#frmSearch .download').attr('value', null);
$('.pagination a').click(function (e) {
e.preventDefault();
var url = this.href;
var len = url.length;
var pos = url.indexOf('?page=') + 6;
var page = url.substr(pos, len - pos);
$('#frmSearch .pageNo').attr('value', page);
$('#frmSearch .download').attr('value', null);
return $('#frmSearch').submit();
});
$('#frmSearch .pageSize').change(function () {
$('#frmSearch .pageNo').attr('value', 1);
$('#frmSearch .download').attr('value', null);
return $('#frmSearch').submit();
});
$("#btnReset").click(function (e) {
$("#frmSearch").find("input[type=text],select").each(function() {
$(this).val("");
});
});
});
</script>
@if (TempData["msg"] != null)
{
<div><b>@(TempData["msg"].ToString())</b></div>
}
@using (Html.BeginForm(ViewContext.RequestContext.RouteData.Values["action"].ToString(),
ViewContext.RequestContext.RouteData.Values["controller"].ToString(),
FormMethod.Post,
new { @class = "form-inline", id="frmSearch", name="frmSearch"}))
{
<div class="well" id="SearchFilters">
<fieldset>
@foreach (ModelMetadata filter in filters)
{
if (filter.PropertyName.Trim().ToLower() == "expertise")
{
<label for="@ViewData.ModelMetadata.PropertyName" style="vertical-align:middle;">@(filter.DisplayName):
@Html.Raw("<br/>" + SiteHelper.GetUserProfileExpertiseListAsHtml(ViewData["Expertise"].ToString()) + "<br/><br/>")
</label>
}
else if (filter.PropertyName.Trim().ToLower() == "subjectareas")
{
<label for="@ViewData.ModelMetadata.PropertyName" style="vertical-align:middle;">@(filter.DisplayName):
@Html.Raw("<br/>" + SiteHelper.GetUserProfileSubjectAreaListAsHtml(ViewData["SubjectAreas"].ToString()) + "<br/>")
</label>
}
else
{
<label for="@ViewData.ModelMetadata.PropertyName" style="width:250px;vertical-align:top;">@(filter.DisplayName):
@switch (filter.TemplateHint.Trim().ToLower())
{
case "string":
@Html.TextBox(filter.PropertyName, filter.AdditionalValues[Globals.DefaultValueKey])
break;
case "dropdown":
var items = filter.AdditionalValues[Globals.DropdownAttributeValuesKey] as IEnumerable<Acceleron.Domain.SelectListItemDTO>;
var selectList = new SelectList(items, "Value", "Text", filter.AdditionalValues[Globals.DefaultValueKey] as string);
@Html.DropDownList(filter.PropertyName, selectList)
;
break;
case "date":
@Html.TextBox(filter.PropertyName, filter.AdditionalValues[Globals.DefaultValueKey], new {@class = "date", title = filter.DisplayName})
break;
}
</label>
}
}
@if (filters != null && filters.Count > 0)
{
<div class="clear"></div>
<div class="float-right">
@* <button type="submit" id="btnSearch" name='Command' class="btn btn-inverse" value="Search"> Search </button>
<button type="button" id="btnReset" name="Command" class="btn" value="Clear">Clear</button>*@
<button type="submit" id="btnSearch" name='btnSearch' class="btn btn-inverse"> Search </button>
<button type="button" id="btnReset" name="btnReset" class="btn">Clear</button>
</div>
<div class="clear"></div>
}
<span class="pull-right" style="padding-top:5px;">
@if (totalItemCount > 0 &&
ViewContext.RequestContext.RouteData.Values["controller"].ToString().Trim().ToLower() != "connect")
{
@*<button class="download" type="submit" id='btnDownload' name='Command' class="btn btn-primary ignore" value="Download">Download</button>*@
<button type="submit" id='btnDownload' name='btnDownload' class="btn btn-primary ignore" value="">Download</button>
}
@if (ViewContext.RequestContext.RouteData.Values["controller"].ToString().Trim().ToLower() != "report" &&
ViewContext.RequestContext.RouteData.Values["controller"].ToString().Trim().ToLower() != "connect")
{
<a href="/admin/@ViewContext.RequestContext.RouteData.Values["controller"].ToString()/edit" class="btn btn-warning">Add</a>
}
@if (ViewContext.RequestContext.RouteData.Values["controller"].ToString().Trim().ToLower() == "user" || ViewContext.RequestContext.RouteData.Values["controller"].ToString().Trim().ToLower() == "coupon")
{
<a href="/admin/@ViewContext.RequestContext.RouteData.Values["controller"].ToString()/upload" class="btn btn-warning">Upload</a>
}
</span>
@Html.Hidden("PageIndex", @pageIndex, new { @class = "pageNo" })
@Html.Hidden("SortColumn", @sortColumn)
@Html.Hidden("SortDirection", @sortDir)
</fieldset>
</div>
if (totalItemCount > 0)
{
<div class="pagination">
<ul>
@Html.Raw(@Html.Pager(@pageSize, @pageIndex, @totalItemCount))
</ul>
<span class="pull-right">Page Size:
<select id="PageSize" name="PageSize" style="width: 50px; margin-top: 3px;" class="pageSize">
<option value="25" @if (pageSize == 25)
{ @Html.Raw("Selected");
}>25</option>
<option value="50" @if (pageSize == 50)
{ @Html.Raw("Selected");
}>50</option>
<option value="100" @if (pageSize == 100)
{ @Html.Raw("Selected");
}>100</option>
<option value="200" @if (pageSize == 200)
{ @Html.Raw("Selected");
}>200</option>
<option value="300" @if (pageSize == 300)
{ @Html.Raw("Selected");
}>300</option>
<option value="500" @if (pageSize == 500)
{ @Html.Raw("Selected");
}>500</option>
</select>
</span>
</div>
}
else
{
@Html.Hidden("PageSize", "25", new { @class = "pageSize" })
}
}
<iframe id="download_target" name="download_target" src="" style="display:none; width: 0; height: 0;
border: 0px solid #fff;"></iframe>
&#13;