>我有以下操作方法
public ViewResult Index()
public ActionResult Edit(decimal id)
当用户单击"编辑"链接时,我想调用Edit
操作。以下是示例代码片段
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ItemID }) |
@Html.ActionLink("Details", "Details", new { id=item.ItemID }) |
@Html.ActionLink("Delete", "Delete", new { id= item.ItemID})
</td>
它重定向到的网址http://MyServer/Orders/Details/0.020
使用此 URL,我的操作方法不会调用。如果我手动编辑 URL 以删除".",那么我的方法就会被调用。
我的问题是传递十进制值以调用 Action 方法的正确方法是什么?
最好的方法是使用查询字符串传递它:
http://MyServer/Orders/Details?id=0.20
您是如何定义路线的?从路由中删除 id,操作链接会将其添加为查询字符串。
尝试使用自定义十进制模型绑定器,这是Phil Haack的一篇好文章
阿巴斯雷 -
public class DecimalModelBinder : IModelBinder {
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext) {
ValueProviderResult valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName);
ModelState modelState = new ModelState { Value = valueResult };
object actualValue = null;
try {
actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
CultureInfo.CurrentCulture);
}
catch (FormatException e) {
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
}
将以下行添加到 system.webServer/handlers 元素中的站点 web.config
<add name="ApiURIs-ISAPI-Integrated-4.0"
path="*"
verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
您可以在视图中编写类型。我将其用于任何类型的类型,并且效果很好。
id=(decimal) item.ItemID