为什么点击@Url.Action 会抛出错误?
Why does clicking @Url.Action throws an error?
当我单击一个按钮输入时触发控制器 ImagePopup
内的一个动作 Stories
但它抛出一个错误。
代码:
@{
var listData = (List<HimHer.Models.Stories>)ViewBag.Grid;
foreach (var imageName in listData)
{
<div class="col-md-4">
@Html.HiddenFor(model => model.Story)
<input
class="img-responsive img-thumbnail"
type="image"
onclick="location.href='@Url.Action("ImagePopup", "Stories", new {story= imageName.Story})'"
src="@("/UploadedFiles/"+ imageName.Image)"
alt="Submit"
width="100%"
height="100%"/>
</div>
}
}
点击输入后会抛出错误:
The resource cannot be found. Requested URL: /Stories/ImagePopup
即使它存在。它就在故事文件夹中。这是没有模型的部分视图。
[HttpPost]
public ActionResult ImagePopup(string story)
{
ViewBag.PopupStyle = "";
ViewBag.PopupStory = story;
return View("GetImagesStories");
}
我做错了什么?
我相信它正在寻找 HTTPGet 操作。
如果你想调用你的 post,你需要使用 HTML.BeginForm,但如果页面上有太多,它会变得很麻烦。
尝试为您的 ImagePopup
操作方法使用 [HttpGet]
属性
设置当前页面的href位置:
location.href=
使浏览器执行 Get 请求类型,并且不会 post 将任何表单数据返回给您的控制器。因为你的控制器上的方法只适用于 post 请求(因为 [HttpPost]
属性)没有其他匹配的方法可以工作,因此你得到一个异常。
解决方案:
您可以继续使用Get方法。将 [HttpPost]
替换为 [HttpGet]
将使您完成一半。另一个要求是确保 Url.Action
代码包含所有需要 post 返回的信息(例如 @Html.HiddenFor(model => model.Story)
中的所有数据不包括在内,我不不知道你需不需要)。
或
您可以修改代码以使用 Post 方法。将您的 <input type="image"
更改为 <button type="submit">
并在每个按钮和隐藏的输入元素周围添加一个表单。
当我单击一个按钮输入时触发控制器 ImagePopup
内的一个动作 Stories
但它抛出一个错误。
代码:
@{
var listData = (List<HimHer.Models.Stories>)ViewBag.Grid;
foreach (var imageName in listData)
{
<div class="col-md-4">
@Html.HiddenFor(model => model.Story)
<input
class="img-responsive img-thumbnail"
type="image"
onclick="location.href='@Url.Action("ImagePopup", "Stories", new {story= imageName.Story})'"
src="@("/UploadedFiles/"+ imageName.Image)"
alt="Submit"
width="100%"
height="100%"/>
</div>
}
}
点击输入后会抛出错误:
The resource cannot be found. Requested URL: /Stories/ImagePopup
即使它存在。它就在故事文件夹中。这是没有模型的部分视图。
[HttpPost]
public ActionResult ImagePopup(string story)
{
ViewBag.PopupStyle = "";
ViewBag.PopupStory = story;
return View("GetImagesStories");
}
我做错了什么?
我相信它正在寻找 HTTPGet 操作。 如果你想调用你的 post,你需要使用 HTML.BeginForm,但如果页面上有太多,它会变得很麻烦。
尝试为您的 ImagePopup
操作方法使用 [HttpGet]
属性
设置当前页面的href位置:
location.href=
使浏览器执行 Get 请求类型,并且不会 post 将任何表单数据返回给您的控制器。因为你的控制器上的方法只适用于 post 请求(因为 [HttpPost]
属性)没有其他匹配的方法可以工作,因此你得到一个异常。
解决方案:
您可以继续使用Get方法。将 [HttpPost]
替换为 [HttpGet]
将使您完成一半。另一个要求是确保 Url.Action
代码包含所有需要 post 返回的信息(例如 @Html.HiddenFor(model => model.Story)
中的所有数据不包括在内,我不不知道你需不需要)。
或
您可以修改代码以使用 Post 方法。将您的 <input type="image"
更改为 <button type="submit">
并在每个按钮和隐藏的输入元素周围添加一个表单。