【发布时间】:2015-03-14 22:45:29
【问题描述】:
提交表单时调用了uploadimage操作,但前面有一个id,可能是我的路由配置不对?
它正在提交 managespace/4/uploadimage 呈现的 html 是这样的
<form action="/ManageSpaces/4/UploadImage"
路由配置
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces"}, //removed action = "overview" from default - this would make it optional, now its mandatory
new { id = @"\d+" } //The regular expression \d+ matches one or more integers
);
jquery
$('#selectedFile').change(function() {
$('#imageajaxForm').submit();
});
<div id="images">
@using (Ajax.BeginForm("UploadImage", "ManageSpaces", new { id = Model.SpaceId }, new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "imageList"
}, new { id = "imageajaxForm" }))
{
@*@Html.TextBoxFor(model => model.TeacherImageFileBase, new { @type = "file", id = "selectedFile", style = "display: none;" })*@
<input type="file" id="selectedFile" style="display: none;" />
<input type="button" value="Add Photos" class="btn" id="pictureupload" />
}
<div id="imageList">
@foreach (var image in Model.Images)
{
<h4>this is an image</h4>
}
</div>
</div>
控制器中的动作方法
[HttpGet]
public ActionResult UploadImage(int id)
{
return Json("Saved");
}
【问题讨论】:
-
你没有
id="imageajaxForm"的控件 -
new { name = "imageajaxForm" } 用 id 试了一下,还是不行
-
检查你生成的html。
new { id = "imageajaxForm" }(Ajax.BeginForm()的最后一个参数)应该是<form .... id="imageajaxForm" ...> -
现在你已经完全改变了问题!您使用
new { id = Model.SpaceId }指定id路由参数,并将路由定义为ManageSpaces/{id}/{action},这导致action="/ManageSpaces/4/UploadImage"(假设值为SpaceId = 4)。您希望网址看起来如何?
标签: jquery html asp.net-mvc asp.net-ajax