[RESOLVED]how can set parameter values for params string[] parameter

 Hi dear ,

I
am
required to
produce
the following address

http://localhost:1531/Content/Details?cid=34&query=%D8%AA%D8%AD&positions%5B0%5D=true&positions%5B0%5D=false&positions%5B1%5D=false&positions%5B2%5D=false

this url after decode is :

http://localhost:1531/Content/Details?cid=32&query=تحلیل&positions[0]=true&positions[0]=false&positions[1]=false&positions[2]=false

My action
method contains
the following
parameters

  public ActionResult Details(string cid, string query,  int? page, int id = 0, params string[] positions)
         {

---------
//set view bags
 if(positions != null)
            {
                ViewBag.Title = positions[0];
                ViewBag.Description = positions[1];
                ViewBag.KeyWord = positions[2];
           
            }
}
}

But
when
I set
the parameters
required for
the positions
parameter
I get
an error :

In fact i cant  use positions[0] , positions[1] and positions[2] !!!

my cshtml page :

       

        title  
        @Html.CheckBox("positions[0]", ViewBag.Title as string)  

        description
        @Html.CheckBox("positions[1]", ViewBag.Description as string)  

       keyword 
        @Html.CheckBox("positions[2]", ViewBag.KeyWord as string)  

 

@Html.PagedListPager(Model, page => Url.Action("Details", new {cid = ViewBag.currentFilter, query= ViewBag.currentSearchString , positions[0]=ViewBag.title , positions[1]=ViewBag.Description ,position[2]=ViewBag.KeyWord,page }))
    

how can i set values for positions parameter in cshtml page ?

How can i set position[0] ,positions[2] and positions[3] … i want know this just

the rest of my code is correct

please help me

if the action like this

public ActionResult Details(string cid, string query, int? page, int id = 0, IDictionary<string, string> positions)
        {

            if (positions != null)
            {
                ViewBag.Title = positions["Title"];
                ViewBag.Description = positions["Description"];
                ViewBag.KeyWord = positions["KeyWord"];
            }
        }

view can be like this

 	@{
                IDictionary<string,string> positions1=new Dictionary<string,string>();
                positions.Add("cid",ViewBag.currentFilter as string);
                positions.Add("query",ViewBag.currentSearchString as string);
                positions.Add("title",ViewBag.title as string);        
                Html.PagedListPager(Model, page => Url.Action("Details", positions))
            }

You can set ViewData["positions[0]"]  in your controller

Hi nazlin,

Please remove params keyword.

public ActionResult Details(string cid, string query,  int? page, int id = 0, string[] positions)

To get the request data, you could use this code below:

@Request.Params["positions[0]"]

Best Regards

Starain

Leave a Reply