Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My default route is
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

Now in the controller, I have the method.
public ActionResult LanguageEdit(int id)
       {

The corresponding code in the view is:
<div class="widget-content">
                       @using (Html.BeginForm("Language", "Country", FormMethod.Post, new { enctype = "multipart/form-data" }))
                       {
                           @Html.HiddenFor(m => m.CountryId)
                           @Html.Grid(Model.Languages).Columns(column =>
                      {
                          column.For(m => m.LanguageName).Named("Languages");
                          column.For(m => String.Format("<a class='btn btn-primary' href='/Country/LanguageEdit/{0}'>Edit</a> <a href='#deleteConfirmModal' role='button' data-toggle='modal' class='btn btn-danger deleteBtn'><input type='hidden' id='countryId' value='{0}'/>Remove</a>", m.CountryId)).Named("Actions").Encode(false);
                      }).Attributes(@class => "table")
                       }
                   </div>

We pass id (CountryId) to the method.
However for some reason, I have to pass another parameter languageId to the controller method.

So what is the best way?

What I have tried:

tried this, but still not clear. Need code assistance. Specifically in the view.
Posted
Updated 13-Jul-17 4:06am
Comments
F-ES Sitecore 12-Jul-17 10:52am    
If it's part of the model use the same code you have for CountryId. If it's not then use

@Html.Hidden("languageId", languageId)

Read it using normal model binding or Request.Form["languageId"]
Member 12658724 12-Jul-17 12:08pm    
Do I have to change the route?
F-ES Sitecore 12-Jul-17 12:10pm    
Nope. The route is when you want to get values from the url structure (as opposed to querystring values), you are passing your values as form fields.
Member 12658724 12-Jul-17 12:24pm    
I am struggling with the route url: "{controller}/{action}/{id}". It has only one parameter but I have to pass two parameters.
F-ES Sitecore 13-Jul-17 4:15am    
No need to change the route, just add this to the form

@Html.Hidden("languageId", languageId)

then add a languageId param to your Language action

public ActionResult Language(YourModel model, int? languageId)

the languageId param will then be populated with that hidden value. I've assumed it is an int, change the type as neccessary

In my opinion the simple way for passing a collection of parameters is create a class that has as many properties as "parameters" do you want.
For example:
public class model1
{
	public int param1;
	public int param2;
	...       
}


And then your controller should be declared as:
public ActionResult LanguageEdit(model1 model)
{
	...
		model1.param1
		...
		model2.param2
	...
}


And inthe view something like:
@Html.HiddenFor(m => m.param1)
    ...
@Html.TextFor(m => m.param2)


This kind of "class" can be extended to any number of "parameters" of any kind (or type)
 
Share this answer
 
Sorry I didn't realise you were not submitting the form but rendering a link to click. You'll need to do something like this

column.For(m => String.Format("<a class='btn btn-primary' href='/Country/LanguageEdit/{0}?LanguageId={1}'>Edit</a> <a href='#deleteConfirmModal' role='button' data-toggle='modal' class='btn btn-danger deleteBtn'><input type='hidden' id='countryId' value='{0}'/>Remove</a>", m.CountryId, m.LanguageId)).Named("Actions").Encode(false);


Then change your controller action

public ActionResult LanguageEdit(int id, int? languageId)
        {


"id" will be populated via routing, and languageId populated via standard querystring values
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900