Link to home
Start Free TrialLog in
Avatar of Charles Sugden
Charles SugdenFlag for United States of America

asked on

How to create a dynamic ActionResult

I have several menu items implemented as buttons.
Each menu item will hide or show a specific group of entry choices.
Once the choices are made the user clicks a continue button.

The code I have written doesn't scale well as the number of menu items increases ( as in my attachment)

I would like a better approach to implementing this logic. It seems I cannot dynamically create the action link parameters.

Any help in understanding the issues would be greatly appreciated
navigate.txt
Avatar of HainKurt
HainKurt
Flag of Canada image

try something like this

<button class="action" id=action1 action="action1">Action 1</button>
<button class="action" id=action2 action="action2">Action 2</button>
...
<button class="action" id=actionN action="actionN">Action N</button>

$(".action").on("click",function(){
  var action = $(this).attr("action");
  location.href = '@Url.Action(' + action + ', "myController")?dateFrom=' + startDate + '&dateTo=' + endDate ;
});

Open in new window

Avatar of Charles Sugden

ASKER

You know that's what I had tried. The error message is
'Too many characters in character literal'

It treats the action variable as part of the text because of when the substitution occurs with the RAZOR text I think
you have to wrap all with "

I dont see any issue here...

<button class="action" id=action1 action="action1">Action 1</button>
<button class="action" id=action2 action="action2">Action 2</button>
...
<button class="action" id=actionN action="actionN">Action N</button>

$(".action").on("click",function(){
  var action = $(this).attr("action");
  var url = '@Url.Action(' + action + ', "myController")?dateFrom=' + startDate + '&dateTo=' + endDate;
  console.log(url);
  //location.href = url; //un-comment this if it is ok
});

Open in new window


what do you get on sonsole after click? Press F12 - console...
That code does not compile. Here is a screenshot of the error message.
Error.png
what about this

function navigate()
{
  var action =  $('#choice').val();

  //location.href = '@Url.Action("action1", "myController")?dateFrom=' + startDate + '&dateTo=' + endDate ;
  location.href = '@Url.Action("' + action + '", "myController")?dateFrom=' + startDate + '&dateTo=' + endDate ;
}

Open in new window


if your org code works, this should work too...
and this is fixed code for my suggestion above

<button class="action" id=action1 action="action1">Action 1</button>
<button class="action" id=action2 action="action2">Action 2</button>
...
<button class="action" id=actionN action="actionN">Action N</button>

$(".action").on("click",function(){
  var action = $(this).attr("action");
  var url = '@Url.Action("' + action + '", "myController")?dateFrom=' + startDate + '&dateTo=' + endDate;
  console.log(url);
  //location.href = url; //un-comment this if it is ok
});

Open in new window

What strikes about your solution is that when you append the action variable, .NET does the show it in 'white' ionstead it shows it in the color of my other text.

Also the action context is stored within the value attribute of the input statement, using your code, I have examined the content and it says Object HTMLCollection

            <button type="button" id="getMyDashboardJ" value="dashboardJ">Test Dashboard</button>
                var actionx = $(this).attr("action");
                alert('clicking'+actionx);

               var url = '@Url.Action("' + actionx + '", "myController")?dateFrom=' + startDate + '&dateTo=' + endDate + '&subject=' + selectedIndices + '&alerttype=Alert' + '&alertcategory=' + selectedCategory;
                console.log(url);

/myController/&#39;%20%2b%20actionx%20%2b%20&#39;?dateFrom=05/01/2017&dateTo=05/06/2017&subject=AlarmNet-I Degrade,&alerttype=Alert&alertcategory=
I am a bit lost here...

what is this? is it a function?

@Url.Action("actionx", "myController")

also here,
/myController/&#39;%20%2b%20actionx%20%2b%20&#39;?dateFrom=05/01/2017&dateTo=05/06/2017&subject=AlarmNet-I Degrade,&alerttype=Alert&alertcategory=

Open in new window


how that one is transferred into this

@Url.Action("actionx", "myController")
>>>
/myController/&#39;%20%2b%20actionx%20%2b%20&#39;

are you using some url re-write?
I am not sure what you mean by url-reqrite other than to say that I must compose the url at runtime based upon what button the user has chosen. each button contains (in the value property) the name of the method within the controller (myController) to execute. The additional information coming from other html inputs is appended onto the url as well.

I rename you action variable because I already  had an action variable named that.

Tonight I will isolate your solution and determine its merits
I have created an isolated view with a slight variation of your code within:

html:
           <button class="action" id=action1 action="action1">Action 1</button>

Jquery:
    $(".action").on("click", function () {
        alert('here');
        var action = $(this).attr("action");
        var startDate = '5/1/2017';
        var selectedIndices = 'AM Daily';
        var selectedCategory = '';
        var endDate = '5/10/2017';

        var url = '@Url.Action("' + action + '", "myController")?dateFrom=' + startDate + '&dateTo=' + endDate + '&subject=' + selectedIndices + '&alerttype=Alert' + '&alertcategory=' + selectedCategory;
        console.log(url);
        location.href = url; //un-comment this if it is ok
    });

resulting url:
http://localhost:57097/myController/'%20%2b%20action%20%2b%20'?dateFrom=5/1/2017&dateTo=5/10/2017&subject=AM Daily&alerttype=Alert&alertcategory=



I cant see you the substitution could ever succeed.
perhaps I was not clear to you on my requirement
what it should be? is this?

http://localhost:57097/myController/action?dateFrom=5/1/2017&dateTo=5/10/2017&subject=AM Daily&alerttype=Alert&alertcategory=
I am getting this and it looks ok to me...

@Url.Action("action1", "myController")?dateFrom=5/1/2017&dateTo=5/10/2017&subject=AM Daily&alerttype=Alert&alertcategory=

from the code @ ID: 42127722
The content that the action keyword contains is not being sent into the url.
I was expecting

http://localhost:57097/myController/'%20%2b%20action1%20%2b%20'?dateFrom=5/1/2017&dateTo=5/10/2017&subject=AM Daily&alerttype=Alert&alertcategory=
are you sure you have this

<button class="action" id=action1 action="action1">Action 1</button>
I pasted it from the code itself
do you have a link to test

I dont get how you end up with "action"

var action = $(this).attr("action");

should give you "action1"!
It does give you action1, the substitution just doesnt happen, I think because the razor substitution happens at a different time than the jquery execution??? or becaue the razor doesnt like expressions in the actiolink argument?
ASKER CERTIFIED SOLUTION
Avatar of Charles Sugden
Charles Sugden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After my own research I found that most people pursued a different approach