littlegeek
littlegeek

Reputation:

Passing multiple parameters to a controller?

ok. simple one that is wrapping my brain

I have a method that I have in the controller

public ActionResult Details(string strFirstName, string strLastName)
{
      return View(repository.getListByFirstNameSurname(strFirstName, strLastName)
}

How do i get multiple parameters from the URL to the controller?

I dont want to use the QueryString as it seems to be non-mvc mind set.

Is there a Route? Or Other mechanism to make this work? Or am I missing something altogehter here with MVC

EDIT

the url that I am trying for is

http://site.com/search/details/FirstName and Surname

so if this was classic asp

http://site.com/search/details?FirstName+Surname

But i feel that i have missed understood something which in my haste to get to working code, I have missed the point that there really should be in a put request - and I should collect this from the formcollection.

Though might be worth while seeing if this can be done - for future reference =>

Upvotes: 7

Views: 35569

Answers (5)

satish
satish

Reputation: 101

For example, suppose that you have an action method that calculates the distance between two points:

public void Distance(int x1, int y1, int x2, int y2)
{
   double xSquared = Math.Pow(x2 - x1, 2);
   double ySquared = Math.Pow(y2 - y1, 2);
   Response.Write(Math.Sqrt(xSquared + ySquared));
}

Using only the default route, the request would need to look like this:

/simple/distance?x2=1&y2=2&x1=0&y1=0

We can improve on this by defining a route that allows you to specify the parameters in a cleaner format.

Add this code inside the RegisterRoutes methods within the Global.asax.cs.

routes.MapRoute("distance",
"simple/distance/{x1},{y1}/{x2},{y2}",
new { Controller = "Simple", action = "Distance" }
);

We can now call it using /simple/distance/0,0/1,2

Upvotes: 10

Dushantha
Dushantha

Reputation: 103

I also had the same problem once and what I did was use the Ajax call inside the jQuery function. First I selected all parameter values using jQuery selectors. Below is my jQuery function.

<script language="javascript" type="text/javascript"> 
 $(document).ready(function () {
    $('#btnSendNow').click(function () {
    var grid = $('#Patient-kendo-Grid').data('kendoGrid');
    var location = $('#EmailTempalteLocation option:selected').text();
    var appoinmentType = $('#EmailTemplateAppoinmentType option:selected').text();
    var emailTemplateId = $('#EmailTemplateDropdown').val();
    var single = $('input:radio[name=rdbSingle]:checked').val();
    var data = grid.dataSource.view();
    var dataToSend = {
    patients: data,
    place: location,
    appoinment: appoinmentType,
    rdbsingle: single,
    templateId: emailTemplateId
    };
    debugger;
    $.ajax({
    url: 'Controller/Action',
    type: 'post',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(dataToSend)
    });
    });
    });
</script>

My controller method has five parameters and it is as below.

[HttpPost]
public ActionResult SendEmailToMany(List<PatientModel> patients, string place, string appoinment, string rdbsingle, string templateId)
{ 
emailScheduleModel = new EmailScheduleModel();
AmazonSentEmailResultModel result;
List<string> _toEmailAddressList = new List<string>();
List<string> _ccEmailAddressList = new List<string>();
List<string> _bccEmailAddressList = new List<string>();
IEmailTemplateService emailTemplateService = new EmailTemplateService();
EmailTemplateContract template = emailTemplateService.GetEmailTemplateById(new       Guid(templateId));
emailScheduleModel.EmailTemplateContract = new EmailTemplateContract();
emailScheduleModel.EmailTemplateContract = template;
}

It is working fine in my developments.

For further details please follow the below url.
http://dushanthamaduranga.blogspot.com/

Upvotes: -1

San
San

Reputation: 2346

Use hidden values in your form

<%= Html.Hidden("strFirstName", Model.FirstName)%>
<%= Html.Hidden("strLastName", Model.LastName)%>

and the model binder will do the binding

public ActionResult Details(string strFirstName, string strLastName)
{
      return View(repository.getListByFirstNameSurname(strFirstName, strLastName)
}

Upvotes: 1

eu-ge-ne
eu-ge-ne

Reputation: 28153

Something like this?:

routes.MapRoute("TheRoute",
    "{controller}/{action}/{strFirstName}/{strLastName}",
    new { controller = "Home", action = "Index", strFirstName = "", strLastName = "" }
);

or:

routes.MapRoute("TheRoute2",
    "people/details/{strFirstName}/{strLastName}",
    new { controller = "people", action = "details", strFirstName = "", strLastName = "" }
);

UPDATED:

This route should be placed before "Default" route:

// for urls like http://site.com/search/details/FirstName/Surname
routes.MapRoute("TheRoute",
    "search/details/{strFirstName}/{strLastName}",
    new { controller = "search", action = "details", strFirstName = "", strLastName = "" }
);

routes.MapRoute("Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

Upvotes: 2

Peter J
Peter J

Reputation: 57958

It is also possible to use FormCollection:

public ActionResult Details(int listId, FormCollection form)
{
  return View(rep.getList(form["firstName"], form["lastName"])
}

Likewise, if the HTTP request contains a form value with the exact same name (case sensitive), it will automatically be passed into the ActionResult method.

Also, just to be clear, there is nothing un-MVC about querystring parameters.

Upvotes: 1

Related Questions