【发布时间】:2013-02-08 10:22:23
【问题描述】:
所以,我有一个接受字符串和对象的方法,该对象具有 MVC 转换为查询字符串参数的值,我的问题是在哪里以及如何摆脱空参数,以便我的 url 更清晰。
表格:
@using (@Html.BeginForm("Index", "ControllerName", FormMethod.Get,
new { enctype = "multipart/form-data", id = "form2" }))
//Should I do a check in here for null values before getting the request?
路由链接:
routes.MapRoute
(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
类:
class formModel{
public string name {get;set;}
public int? age {get;set;}
public Guid? jobId{get;set;}
public string Fullname {get;set;}
}
对象属性:
formModel{
name: "Mike",
age: 29,
jobId: null,
Fullname: ""
}
控制器动作:
[HttpGet]
public ActionResult Index(string sortByText, SearchFormModel formModel)
{
var model = new SomeViewModel();
model.FormModel = formModel;
//etc
return View(model);
}
网址:
示例:http://www.domain.com/mycontroller?name=Mike&age=29&jobId=&Fullname=&Find=Find
我怎样才能摆脱 jobId 和 Fullname 和 Find?
【问题讨论】:
-
一种可能性是使它可以为空而不是通过它。另一个是传递一个像 0 这样的虚拟值。
-
我现在正在调查 Global.asax 的路由,看看我是否可以在那里做点什么。
-
它不像你的动作方法参数那样在路由中。接受一个可以为空的 id,或者通过将 id 默认为 0 来使 id 可选。
-
是否可以在 Get 请求中传递对象?
-
是的,它使用对象工作,但我只是想摆脱用户在搜索中没有输入的额外参数。
标签: asp.net-mvc asp.net-mvc-routing query-string