R
R
Roman Gamretsky2014-01-20 00:27:57
ASP.NET
Roman Gamretsky, 2014-01-20 00:27:57

How to get one of the passed parameters?

Using ActionLink I pass several parameters:

@Html.ActionLink("Вход", "Dialog", null,

                               new
                               {
                                   @class = "openDialog",
                                   data_dialog_id = "entry",
                                   data_dialog_title = "Вход",
                                   data_dialog_width = "auto",
                                   data_dialog_height = "auto"
                               })

How to get only the parameter data_dialog_id = "entry" in the controller?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Valery Abakumov, 2014-07-01
@rgamretsky

Good afternoon!
Perhaps the question is still relevant, so I will answer.
To pass a parameter to an action method, you need the action method itself to accept the parameter (as chydaya correctly pointed out ):

public ActionResult Dialog(string dataDialogId)
{
  //...
  return View();
}

It is not necessary to specify parameter values ​​in the route. Those. route can be standard:
routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}",
  defaults: new { controller = "Home", action = "Index" }
);

If you use Visual Studio for development, then when you enter "Html.ActionLink(" you immediately trigger IntelliSense, which tells you what input parameters this method has. And if you look closely, then, as Vadimyan correctly noted , it has various overloads, and some have an object routeValues ​​parameter - this is what you need.It is in this parameter that you specify the variables for the action method, for example, like this:
In your example you provided in the question:
@Html.ActionLink("Вход", "Dialog", null,

                               new
                               {
                                   @class = "openDialog",
                                   data_dialog_id = "entry",
                                   data_dialog_title = "Вход",
                                   data_dialog_width = "auto",
                                   data_dialog_height = "auto"
                               })

You are setting html attributes , and you have null as route variables . The generated html markup will look like this:
Take the time to learn MVC inside and out with the book " ASP.NET MVC3 Framework with C# Examples for Professionals ". This is the foundation, and even on asp.net you will not find such detailed information on how ASP.NET MVC works and how to work correctly. I used the tutorials (more on WebForms + EntityFramework) from the asp.net site , but they are aimed at "read the tutorial and start doing". However, if you want to competently develop in ASP.NET MVC, start by learning the basics and foundation.
I hope I helped you a little.
Good luck!

V
Vadim Martynov, 2014-01-20
@Vadimyan

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- имя контроллера
                new { id = article.ArticleID }, // <-- Передаваемые параметры.
                null  // <-- htmlArguments, эта коллекция служит для назначения параметров элементу html, который будет сгенерирован.
                )

That is, you need to use not 4, but 3 method parameters. At the same time, the corresponding option of parameters should be registered in the routes (I gave an example with two):
routes.MapRoute(
                name: "Default2",
                url: "{controller}/{action}/d={data_dialog_id}a={data_dialog_title}",
                defaults: new { controller = "Home", action = "Index", data_dialog_id = UrlParameter.Optional, data_dialog_title = UrlParameter.Optional }
            );

If I understand the question correctly...

C
chydaya, 2014-05-13
@chydaya

First, no parameters in your code are passed anywhere except to the link attributes.
Secondly, if you need one parameter, then pass one parameter

@Html.ActionLink("Вход", "Dialog", new { data_dialog_id = "entry" },

                               new
                               {
                                   @class = "openDialog",
                                   data_dialog_id = "entry",
                                   data_dialog_title = "Вход",
                                   data_dialog_width = "auto",
                                   data_dialog_height = "auto"
                               })
                 
public ActionResult Index(string data_dialog_id){
...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question