[英]Cannot link back to MVC view via javascript
I have an Index View and when I click the Edit button, I post to the Edit View (via the Controller) and display a bootstrap modal popup. 我有一个索引视图,当我单击“ 编辑”按钮时,我发布到“ 编辑视图”(通过Controller)并显示引导程序模式弹出窗口。
By posting to the Edit View, the Controller/View automatically handle getting and displaying the correct data on the modal popup. 通过发布到“ 编辑”视图,Controller / View会自动处理如何在模式弹出窗口中获取并显示正确的数据。
Once I'm on my Edit View with the dialog box appearing and I click on the Close button, I simply want to link back to the Index page again; 一旦进入对话框的“ 编辑视图”,然后单击“ 关闭”按钮,我只想再次链接回 “ 索引”页面; but instead, am getting an error with the path of the url.
但相反,URL路径出错。 The new path I want to link to is being " tacked on " to the original path instead of replacing it.
新的路径我要链接到被“ 上涨了 ”原来的路径,而不是取代它。
I'm using the Url.Action method inside the click event of the Close button (of which I verified it's hitting) and have verified the location.href url is exactly what is in the url variable as you see in the code. 我在关闭按钮的click事件中使用了Url.Action方法(已验证其点击效果),并且已经验证了location.href url就是您在代码中看到的url变量中的内容。
What do I need to do to correctly link back to the Index url? 我需要怎么做才能正确链接回索引 URL?
<a href="@Url.Action("Edit", "Category", new { area="Categories", id = item.CategoryID })"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span>Edit</a>
// GET: Categories/Edit/5
public async Task<ActionResult> Edit(short id)
{
if (id == 0)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = await db.GetCategoryIDAsync(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
@model YeagerTechDB.Models.Category
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="modal" id="categoryEditModal" tabindex="-1" role="dialog" aria-labelledby="categoryModal-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="categoryModal-label">Category Description</h4>
</div>
<div class="modal-body">
<div class="form-group">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CategoryDescription, new { @class = "control-label required col-offset-1 col-lg-3 col-md-3 col-sm-3 col-xs-3" })
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
@Html.EditorFor(model => model.CategoryDescription, new { @class = "form-control" } )
@Html.ValidationMessageFor(model => model.CategoryDescription, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" id="btnCloseCategory">Close</button>
<button type="submit" class="btn btn-primary" id="btnSaveCategory">Save</button>
</div>
</div>
</div>
</div>
<div>
@Html.Hidden("categoryEditUrl", Url.Action("Edit", "Category", new { area = "Categories" }))
@Html.Hidden("catID", Model.CategoryID)
</div>
@section Scripts {
<script>
$(document).ready(function ()
{
if (typeof contentEditCategory == "function")
contentEditCategory()
});
</script>
}
$('#btnCloseCategory').click(function (e)
{
var url = '@Url.Action("Index", "Category", new { area = "Categories" })';
location.href = url;
return false;
});
Assuming your javascript is in an external file you could do the following: 假设您的JavaScript位于外部文件中,则可以执行以下操作:
Attach the url to your button within your view with a data attribute as follows: 将url附加到具有数据属性的视图中的按钮,如下所示:
<button type="submit" class="btn btn-default" id="btnCloseCategory"
data-url="@Url.Action("Index", "Category", new { area = "Categories" })">Close</button>
Then pull back the url with the data
method as follows: 然后使用
data
方法拉回url,如下所示:
$('#btnCloseCategory').click(function (e)
{
var url = $(this).data('url');
location.href = url;
return false;
});
尝试将“关闭”按钮的type =“ submit”更改为type =“ button”。
<button type="button" class="btn btn-default" id="btnCloseCategory">Close</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.