asp.net-mvc – MVC传递ID由“”分隔为动作
发布时间:2020-05-24 18:31:43 所属栏目:asp.Net 来源:互联网
导读:我希望有可能通过以下URL类型访问操作: http://localhost/MyControllerName/MyActionName/Id1+Id2+Id3+Id4等 并按以下方式在代码中处理它: public ActionResult MyActionName(string[] ids){ return View(ids);} 是网址中的保留符号.这意味着白色空间.因此,
|
我希望有可能通过以下URL类型访问操作: http://localhost/MyControllerName/MyActionName/Id1+Id2+Id3+Id4等 并按以下方式在代码中处理它: public ActionResult MyActionName(string[] ids)
{
return View(ids);
}
解决方法是网址中的保留符号.这意味着白色空间.因此,为了实现您所寻找的目标,您可以编写自定义模型绑定器:public class StringModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null && !string.IsNullOrEmpty(value.AttemptedValue))
{
return value.AttemptedValue.Split(' ');
}
return base.BindModel(controllerContext,bindingContext);
}
}
然后全局注册string []类型或使用 public ActionResult MyActionName(
[ModelBinder(typeof(StringModelBinder))] string[] ids
)
{
return View(ids);
}
显然,如果你想使用形式为/ MyControllerName / MyActionName / Id1 Id2 Id3 Id4的url将最后一部分绑定为一个名为ids的动作参数,你必须修改使用{id}的默认路由定义. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在ASP.NET MVC 2中的RadioButtonFor
- asp.net-mvc – 特定便携式区域的ControllerFactory
- asp.net web-api – ASP.net Web API RESTful Web服务基本身
- 将列表绑定到asp.net 3.5中的列表视图
- 使用log4net和ASP.NET跟踪会话变量
- asp.net mvc框架,自动发送电子邮件
- asp.net-mvc-3 – ASP.NET MVC 3,Razor Views和便携式区域
- asp.net-mvc – 如何应用css类到mvccontrib网格
- asp.net-mvc – ASP.NET MVC:如何从Html.ActionLink链接中
- 你可以使用asp.net mvc Json()将C#字典转换为Javascript关联
