我使用asp.net mvc4在视图上有一些动态生成的图像...我必须删除所选的
来自视图的图像......但我不知道如何将id从视图传递到控制器
控制器代码:
public ActionResult imagelist(ShirtDb dg)
{
List<ShirtDb> all = new List<ShirtDb>();
using (patternChangeEntities8 et = new patternChangeEntities8())
{
all = et.ShirtDbs.ToList();
}
return View(all);
}
查看代码:
@model List<patternchange.Models.ShirtDb>
@using (Html.BeginForm("Edit", "Patterchange", FormMethod.Post))
{
<table>
@{
int j=0;
for (int i = 0; i < Model.Count(); i += 4) {
j = i;
<tr>
@while(j<i+4&&j<Model.Count())
{
<td>
<img src="data:image/png;base64,@Convert.ToBase64String(Model[j].Image, 0, Model[j].Image.Length)" width="100" onclick="location.href='@Url.Action("Edit", "Patterchange")'" @(Model[j].SId) />
@Html.TextBoxFor(m => Model[j].SId)
<input type="submit" value="delete" />
</td>
j++;
}
<td>
</td>
</tr>
}
}
</table>
}
答案 0 :(得分:0)
您可以尝试使用下面的代码。我创建了一个JavaScript函数,当按钮单击时调用该函数,并且在查看时我在该函数中传递了ID。
您可以通过Ajax调用
调用您的控制器操作查看代码
@model List<patternchange.Models.ShirtDb>
@using (Html.BeginForm("Edit", "Patterchange", FormMethod.Post))
{
<table>
@{
int j=0;
for (int i = 0; i < Model.Count(); i += 4) {
j = i;
<tr>
@while(j<i+4&&j<Model.Count())
{
<td>
<img src="data:image/png;base64,@Convert.ToBase64String(Model[j].Image, 0, Model[j].Image.Length)" width="100" onclick="location.href='@Url.Action("Edit", "Patterchange")'" @(Model[j].SId) />
@Html.TextBoxFor(m => Model[j].SId)
<input type="button" onclick="DeleteImage(@(Model[j].SId))" value="delete" />
</td>
j++;
}
<td>
</td>
</tr>
}
}
</table>
}
<强>的Javascript 强>
<script type="text/javascript">
function DeleteImage(ID) {
$.ajax({
type: "POST",
url: '@Url.Action("Edit", "Patterchange")',
contentType: "application/json; charset=utf-8",
data: "{ id: '"+ID+"' }",
dataType: "json",
success: function () { alert('Success'); },
error: function () { alert('error');}
});
}
</script>
控制器代码
[HttpPost]
public ActionResult Edit(string id)
{
// Write your code for delete record by using id
return View();
}