3

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?

18
  • One possibility is to make it nullable and not pass it. Another is to pass a dummy value like 0. Commented Feb 22, 2013 at 17:58
  • I am looking into the routing of Global.asax now to see if I can do something there. Commented Feb 22, 2013 at 18:04
  • It's not so much in the route as your Action Methods params. Accept a nullable id, or make id optional by defaulting it to 0. Commented Feb 22, 2013 at 18:05
  • Is it possible to pass an object in a Get request? Commented Feb 22, 2013 at 18:10
  • Yup, it works using an object, but I just want to get rid of the extra parameters that the user did not input in the search. Commented Feb 22, 2013 at 18:14

1 Answer 1

0

I ended up filtering the form data with client-side javascript instead which avoids any unnecessary or empty query parameters.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.