我有一个与此相关的页面:
<div class="col-sm-2 displaybutton">
<div data-point-to="displaysHeader">
@using (Html.BeginForm("Administration", "Home", FormMethod.Post))
{
<button id="btnAdministration" type="button" style="width:150px;height:50px;background-color:white;color:blue;border:none;font-size:medium" onclick="return GotoAdminstration();">Administration</button>
}
</div>
</div>
<div class="col-sm-2 displaybutton">
<div data-point-to="pagesHeader">
@using (Html.BeginForm("Indexes", "Meeting", FormMethod.Post))
{
<button id="btnMeetingSchedule" type="submit" style="width:150px;height:50px;background-color:white;color:blue;border:none;font-size:medium" onclick="return ShowOverlay();">Meeting Schedule</button>
}
</div>
</div>
在此页面的JavaScript中:
var user;
$(document).ready(function () {
//debugger;
user = function () { return @Html.Raw(Json.Encode(Model)) }();
然后当我尝试发布时:
function GotoAdminstration() {
ShowOverlay();
var form = $('<form method="POST" action="@Url.Action("Administration","Home")">');
form.append($('<input type="hidden" name="ident" value="' + user + '">'));
$(body).append(form);
form.submit();
}
操作参数始终为null。有什么想法我做错了吗?
答案 0 :(得分:0)
在您的cshtml文件中包含这样的脚本标记
php artisan storage:link
然后尝试在您的javaScript文件中使用它:
<script>
var urlString='@Html.Raw(Url.Action("Administration","Home"))';
</script>
答案 1 :(得分:0)
无需使用整个form
标签作为选择器,只需将id
属性添加到表单并将其用作选择器即可:
查看
@using (Html.BeginForm("Administration", "Home", FormMethod.Post, new { id = "adminform" }))
{
// form contents
}
JS
$('#adminform').submit();
请注意,在提交表单之前append
隐藏的input
元素听起来不是一个好主意(您需要先对其进行反序列化)。尝试在表单内使用viewmodel属性和HiddenFor
帮助程序(我也更喜欢使用jQuery click
事件而不是onclick
事件):
查看
@using (Html.BeginForm("Administration", "Home", FormMethod.Post, new { id = "adminform" }))
{
@Html.HiddenFor(model => model.Ident)
<button id="btnAdministration" type="button" style="width:150px;height:50px;background-color:white;color:blue;border:none;font-size:medium">Administration</button>
}
JS
$('#btnAdministration').click(function () {
ShowOverlay();
$('#Ident').val(user); // add 'user' variable content inside hidden field
$('#adminform').submit();
});