-1

是否可以将隐藏字段值从 Razor 视图传递到内部或标记的控制器?由于该字段是隐藏的,因此将其值传递给 Controller 确实是一个问题。另一方面,我认为传递此隐藏字段的唯一方法是在超链接中使用输入字段。如何创建如下代码?

<a href="/Admin/Delete?ApplicantID=44">
<img class="myclass" src="../../Content/delete.png" title="Delete" />
<input type="hidden" value= "Delete" /></a>


更新

看法:

...
grid.Column("Actions", format: (item) =>
 new HtmlString(
      @Html.ActionImage("../../Content/detail.png", "Detail", "icon-link", "Detail", "Admin", new { applicantId = item.ApplicantID }).ToString() +
      @Html.ActionImage("../../Content/edit.png", "Edit", "icon-link", "Edit", "Admin", new { applicantId = item.ApplicantID }).ToString() +
      @Html.ActionImage("../../Content/delete.png", "Delete", "icon-link", "Delete", "Admin", new { applicantId = item.ApplicantID }).ToString()
 )
)
...


控制器:

[HttpPost]
public ActionResult Delete(int applicantId)
{
    Applicant deletedApplicant = repository.DeleteApplicant(applicantId);
    if (deletedApplicant != null)
    {
        TempData["message"] = string.Format("{0} was deleted",
        deletedApplicant.Name);
    }
        return RedirectToAction("Index");
    }
}


辅助方法:

public static MvcHtmlString ActionImage(this HtmlHelper html, string imagePath, string alt, string cssClass,
       string action, string controllerName, object routeValues)
{
    var currentUrl = new UrlHelper(html.ViewContext.RequestContext);
    var imgTagBuilder = new TagBuilder("img");
    imgTagBuilder.MergeAttribute("src", currentUrl.Content(imagePath));
    imgTagBuilder.MergeAttribute("title", alt);
    imgTagBuilder.MergeAttribute("class", cssClass);
    string imgHtml = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
    var anchorTagBuilder = new TagBuilder("a"); 
    anchorTagBuilder.MergeAttribute("href", currentUrl.Action(action, controllerName, routeValues));
    anchorTagBuilder.InnerHtml = imgHtml; 
    string anchorHtml = anchorTagBuilder.ToString(TagRenderMode.Normal);
    return MvcHtmlString.Create(anchorHtml);
}
4

2 回答 2

1

<input>, <textarea>,<button><select>元素值仅在表单提交期间传递。此外,它们仅在分配name属性时才被传递。

单击锚点不会传递这些值(除非您插入一些 JavaScript 并将其附加到 URL)。最简单的方法是将链接转换为迷你形式:

@using (Html.BeginForm("Delete", "Admin", FormMethod.POST,
                       new { ApplicantID = @Model.ApplicantID }))
{
    <!-- make sure to give this field a name -->
    <input type="hidden" name="???" value="Delete" />
    <input type="image" src="@Url.Content("~/Content/delete.png")" title="Delete" />
}

否则,使用 javascript 并绑定到锚点的单击事件并在继续之前注入隐藏输入的值。

于 2013-11-05T14:22:07.540 回答
0

根据下面的讨论,试试这个。

@Ajax.ActionLink("Delete",
                    "Delete", new { applicantid = item.applicantid },
                    new AjaxOptions
                    {
                        Confirm = "Delete?",
                        HttpMethod = "POST",
                    }, new { @class = "classname" })

a.classname
{
    background: url(~/Content/delete.png) no-repeat top left;
     display: block;
     width: 150px;
     height: 150px;
     text-indent: -9999px; /* hides the link text */
}

注意:这不需要在表单内。

于 2013-11-05T13:56:50.430 回答