我是jquery新手。我想从控制器返回一个值到视图。所以我用viewBag。我尝试逐步向您展示我的所有代码。
我不明白为什么会收到此错误。
这是我的控制器代码:
public ActionResult Index(string accountIdFilter,string dateFilter, int page=1, Sort sortOrder =Sort.Descending)
{
int take = 2;
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = sortOrder == Sort.Descending ? Sort.Assending : Sort.Descending;
ViewBag.AccountIdFilter = accountIdFilter;
DateTime? _dateFilter;
if (dateFilter==null)
{
_dateFilter = null;
}
else
{
long epoch = Convert.ToInt64(dateFilter);
_dateFilter = GetEpoch.FromUnixTime(epoch);
}
ViewBag.Date = dateFilter;
BankDepositHistoryListDTO data;
try
{
data = BankDepositHistoryServices.GetAllByPagination(sortOrder, accountIdFilter, take, _dateFilter, page);
}
catch (Exception ex)
{
logger.Error(ex.ToString());
data = BankDepositHistoryServices.GetAllByPagination(sortOrder, accountIdFilter, take, _dateFilter, 1);
page = 1;
}
var result = data.BankDepositHistoriyItems;
int count = data.Count;
var resultPagedList = new StaticPagedList<BankDepositHistoryDTO>(result, page, take, count);
return View(resultPagedList);
}
主要部分在这里:
ViewBag.Date = dateFilter;
现在我想在我定义视图的输入中得到它。所以我这样做了:
@using (Html.BeginForm())
{
<p>
Find by name: @Html.TextBox("AccountIdFilter", ViewBag.AccountIdFilter as string, new { @Id= "accountIdFilter"})
From date: <input type="text" class="initial-value-type-gregorian-example" id="DateTime" autocomplete="off" />
<input type="hidden" id="customInput" data-value="@ViewBag.Date" />
<input type="submit" value="Search" />
</p>
}
@section scripts
{
<script type="text/javascript" >
var datefilter = $("#customInput").data("value");
debugger
$(document).ready(function () {
console.log("ready!");
$('.initial-value-type-gregorian-example')
.persianDatepicker({
initialValueType: 'gregorian',
locale: 'en',
format:'LLLL',
toolbox: {
submitButton: {
enabled: true
},
calendarSwitch: {
enabled: true
}
},
onSelect: function (unix) {
datefilter = unix;
console.log('datepicker select : ' + unix);
}
})
if (datefilter == null) {
debugger
$("#DateTime").val(null);
}
else {
debugger
$("#DateTime").val(new persianDate.unix(datefilter));
}
});
$("form").submit(function () {
if (datefilter == null) {
window.location.href = "/?accountIdFilter=" + $("#accountIdFilter").val()
debugger
}
else
{
window.location.href = "/?dateFilter=" + datefilter + "&accountIdFilter=" + $("#accountIdFilter").val()
debugger
}
console.log("we are in func")
debugger
return false;
});
</script>
}
主要部分在这里:
var datefilter = $("#customInput").data("value");
if (datefilter == null) {
window.location.href = "/?accountIdFilter=" + $("#accountIdFilter").val()
debugger
}
else
{
window.location.href = "/?dateFilter=" + datefilter + "&accountIdFilter=" + $("#accountIdFilter").val()
debugger
}
我希望得到null或值,但是我在输入中得到了这个值:[object Object]
答案 0 :(得分:0)
将隐藏的输入更改为此:
@Html.Hidden("customInput", (object)ViewBag.Date.ToString("dd.MM.yyyy"), new { @id = "customInput" })
并像这样取它的值:
var myvar = $("#customInput").val();