我有一个剑道菜单,它通过类似的onclick事件绑定到我的表单
@(Html.Kendo().Menu()
.Name("MenuCreate")
.Items(items =>
{
items.Add().Text("<<").Action("Index", "BSfune");
items.Add().Text("New").HtmlAttributes(new{onclick = "getElementById('FormCreate').submit()", @id = "New"});
items.Add().Text("Edit").HtmlAttributes(new { onclick = "getElementById('FormCreate').submit()", @id = "Edit" });
})
.Events(e => e.Select("select"))
)
在我的表单中,我有一个名为FormmMode 的隐藏字段
@using (Html.BeginForm("Create", "BSfune", FormMethod.Post, new { id = "FormCreate" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="form">
<fieldset>
<legend>@ViewBag.Title</legend>
<div>
<div>
(My form code)
</div>
@Html.HiddenFor(model => model.FormMode, new { @id = "FormMode"})
<br />
<br />
<br />
</div>
</fieldset>
</div>
}
我想用所选菜单项文本"新建"或"编辑"设置我的字段表单(FormMode)。我注意到onclick覆盖了选定的事件。所以…它会像这个
<script type="text/javascript">
$(function () {
$('#New').on('click', function () {
$("#FormMode").val($(this).text());
});
});
function select(e) {
}
但这不起作用。。在控制器方面,我有
public ActionResult Create(CrBSfune p_BSfune)
{
(...)
if (p_BSfune.FormMode == "New")
return RedirectToAction("Create");
else
return RedirectToAction("Index");
}
但是我的p_BSfune.FormMode为空。你能帮忙吗?谢谢。:)
明白了!!!捕捉点击事件是解决方案,也是传递值的方法。。那就更棘手了。。但经过几次尝试后,它成功了。:)做到了,效果很好。:)
$('#New').click(function (e) {
e.preventDefault();
$('#FormCreate')[0].FormMode.value = e.target.innerText;
$('#FormCreate').submit();});
不知道这是否是最好的方法。。但它有效。:)