So, I have a method that accepts a string and an object, the object has values that MVC translates into querystring parameters, my question is where and how can I get rid of the parameters that are empty so my url is cleaner.
Form:
@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?
Routing Link:
routes.MapRoute
(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Class:
class formModel{
public string name {get;set;}
public int? age {get;set;}
public Guid? jobId{get;set;}
public string Fullname {get;set;}
}
Object properties:
formModel{
name: "Mike",
age: 29,
jobId: null,
Fullname: ""
}
Controller action:
[HttpGet]
public ActionResult Index(string sortByText, SearchFormModel formModel)
{
var model = new SomeViewModel();
model.FormModel = formModel;
//etc
return View(model);
}
Url:
example: http://www.domain.com/mycontroller?name=Mike&age=29&jobId=&Fullname=&Find=Find
How can I get rid of jobId and Fullname and Find?